Skip to content

Commit

Permalink
完善EventProducer、Touch分发
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajunhui committed Sep 6, 2020
1 parent 856e965 commit e94c735
Show file tree
Hide file tree
Showing 19 changed files with 520 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ demo示例集成了播放控制组件**ControllerCover**、加载中组件**Load
dependencies {
//---------如果仅使用MediaPlayer解码,使用以下依赖。----------
//该依赖仅包含MediaPlayer解码
implementation 'com.kk.taurus.playerbase:playerbase:3.3.8'
implementation 'com.kk.taurus.playerbase:playerbase:3.3.9'
//---------如果使用ExoPlayer解码,使用以下依赖。---------
//该依赖包含exoplayer解码和MediaPlayer解码
//注意exoplayer的最小支持SDK版本为16
implementation 'cn.jiajunhui:exoplayer:338_2117_014'
implementation 'cn.jiajunhui:exoplayer:339_2118_015'
//---------如果使用ijkPlayer解码,使用以下依赖。---------
//该依赖包含ijkplayer解码和MediaPlayer解码
implementation 'cn.jiajunhui:ijkplayer:338_088_009'
implementation 'cn.jiajunhui:ijkplayer:339_088_010'
//ijk官方的解码库依赖,较少格式版本且不支持HTTPS。
implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
# Other ABIs: optional
Expand Down
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ dependencies {
implementation project(':exoplayer')
implementation project(':ijkplayer')
implementation 'com.google.android.material:material:1.0.0'

implementation 'com.github.ctiao:DanmakuFlameMaster:0.9.25'
implementation 'com.github.ctiao:ndkbitmap-armv7a:0.9.21'

}
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public View onCreateCoverView(Context context) {

@Override
public int getCoverLevel() {
return levelLow(1);
return levelMedium(1);
}

@Override
Expand Down
166 changes: 166 additions & 0 deletions app/src/main/java/com/kk/taurus/avplayer/cover/DanmuCover.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.kk.taurus.avplayer.cover;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.kk.taurus.avplayer.R;
import com.kk.taurus.avplayer.play.DataInter;
import com.kk.taurus.playerbase.event.EventKey;
import com.kk.taurus.playerbase.event.OnPlayerEventListener;
import com.kk.taurus.playerbase.log.PLog;
import com.kk.taurus.playerbase.receiver.BaseCover;

import java.util.HashMap;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDisplayer;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;

/**
* @ClassName DanmuCover
* @Description
* @Author Taurus
* @Date 2020/9/6 7:12 PM
*/
public class DanmuCover extends BaseCover {

private IDanmakuView mDanmakuView;
private DanmakuContext mDanmakuContext;

public DanmuCover(Context context) {
super(context);
}

@Override
public void onReceiverBind() {
super.onReceiverBind();

mDanmakuView = findViewById(R.id.layout_danmu_cover_danmu_view);
mDanmakuView.setCallback(new DrawHandler.Callback() {
@Override
public void prepared() {
mDanmakuView.start();
}
@Override
public void updateTimer(DanmakuTimer timer) {

}
@Override
public void danmakuShown(BaseDanmaku danmaku) {

}
@Override
public void drawingFinished() {

}
});

HashMap<Integer, Integer> maxLinesPair = new HashMap<Integer, Integer>();
maxLinesPair.put(BaseDanmaku.TYPE_SCROLL_RL, 5); // 滚动弹幕最大显示5行
// 设置是否禁止重叠
HashMap<Integer, Boolean> overlappingEnablePair = new HashMap<Integer, Boolean>();
overlappingEnablePair.put(BaseDanmaku.TYPE_SCROLL_RL, true);
overlappingEnablePair.put(BaseDanmaku.TYPE_FIX_TOP, true);
mDanmakuContext = DanmakuContext.create();
mDanmakuContext.setDanmakuStyle(IDisplayer.DANMAKU_STYLE_STROKEN, 3).setDuplicateMergingEnabled(false).setScrollSpeedFactor(1.2f).setScaleTextSize(1.2f)
.setMaximumLines(maxLinesPair)
.preventOverlapping(overlappingEnablePair).setDanmakuMargin(40);

mDanmakuView.prepare(new BaseDanmakuParser() {
@Override
protected Danmakus parse() {
return new Danmakus();
}
}, mDanmakuContext);
mDanmakuView.showFPS(true);
mDanmakuView.enableDanmakuDrawingCache(true);
}

@Override
protected View onCreateCoverView(Context context) {
return LayoutInflater.from(context).inflate(R.layout.layout_danmu_cover, null, false);
}

@Override
public int getCoverLevel() {
return levelLow(1);
}

@Override
public void onPlayerEvent(int eventCode, Bundle bundle) {
switch (eventCode){
case OnPlayerEventListener.PLAYER_EVENT_ON_PAUSE:
if (mDanmakuView != null && mDanmakuView.isPrepared()) {
mDanmakuView.pause();
}
break;
case OnPlayerEventListener.PLAYER_EVENT_ON_RESUME:
if (mDanmakuView != null && mDanmakuView.isPrepared() && mDanmakuView.isPaused()) {
mDanmakuView.resume();
}
break;
case OnPlayerEventListener.PLAYER_EVENT_ON_DESTROY:
if (mDanmakuView != null) {
// dont forget release!
mDanmakuView.release();
mDanmakuView = null;
}
break;
}
}

@Override
public void onErrorEvent(int eventCode, Bundle bundle) {
if (mDanmakuView != null && mDanmakuView.isPrepared() && mDanmakuView.isPaused()) {
mDanmakuView.resume();
}
}

@Override
public void onReceiverEvent(int eventCode, Bundle bundle) {

}

@Override
public void onProducerEvent(int eventCode, Bundle bundle) {
super.onProducerEvent(eventCode, bundle);
if(eventCode == DataInter.ProducerEvent.ADD_DANMU_DATA){
PLog.d("DanmuTest_Receive", bundle.toString());
int position = bundle.getInt(EventKey.INT_DATA);
String text = bundle.getString(EventKey.STRING_DATA);
addDanmaku(false, position, text);
}
}

private void addDanmaku(boolean islive, int position, String text) {
if(mDanmakuContext==null)
return;
BaseDanmaku danmaku = mDanmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
if (danmaku == null || mDanmakuView == null) {
return;
}
// for(int i=0;i<100;i++){
// }
danmaku.text = text;
danmaku.padding = 5;
danmaku.priority = 0; // 可能会被各种过滤器过滤并隐藏显示
danmaku.isLive = islive;
danmaku.setTime(position + 1200);
danmaku.textSize = 25f * (getContext().getResources().getDisplayMetrics().density - 0.6f);
danmaku.textColor = Color.RED;
danmaku.textShadowColor = Color.WHITE;
// danmaku.underlineColor = Color.GREEN;
danmaku.borderColor = Color.GREEN;
mDanmakuView.addDanmaku(danmaku);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.kk.taurus.avplayer.event_producer;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.Nullable;

import com.kk.taurus.avplayer.play.DataInter;
import com.kk.taurus.avplayer.utils.RandomUtils;
import com.kk.taurus.playerbase.event.BundlePool;
import com.kk.taurus.playerbase.event.EventKey;
import com.kk.taurus.playerbase.event.OnPlayerEventListener;
import com.kk.taurus.playerbase.extension.BaseEventProducer;
import com.kk.taurus.playerbase.extension.EventCallback;
import com.kk.taurus.playerbase.log.PLog;

import java.util.Random;

/**
* @ClassName DanmuDataProducer
* @Description
* @Author Taurus
* @Date 2020/9/6 6:03 PM
*/
public class DanmuDataProducer extends BaseEventProducer {

private final int MSG_LOAD_DANMU_DATA = 100;

private final int LOAD_DANMU_DATA_PERIOD = 10 * 1000;

private int mCurrStartMs = 1;

private Handler mHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(getSender()==null || getPlayerStateGetter()==null)
return;
switch (msg.what){
case MSG_LOAD_DANMU_DATA:
if(msg.arg1 != 0){
mCurrStartMs = msg.arg1;
}
final int count = new Random().nextInt(20);
for(int i=0;i<count;i++){
Bundle obtain = BundlePool.obtain();
obtain.putInt(EventKey.INT_DATA, mCurrStartMs + (new Random().nextInt(10)*1000));
obtain.putString(EventKey.STRING_DATA, RandomUtils.getRandomLowerLetterStr(RandomUtils.getRandomInt(3,20)));
getSender().sendEvent(DataInter.ProducerEvent.ADD_DANMU_DATA, obtain);
PLog.d("DanmuTest_Send", obtain.toString());
}
mCurrStartMs += LOAD_DANMU_DATA_PERIOD;
mHandler.sendEmptyMessageDelayed(MSG_LOAD_DANMU_DATA, LOAD_DANMU_DATA_PERIOD);
break;
}
}
};

@Override
public void onAdded() {

}

@Override
public void onRemoved() {

}

@Override
public void destroy() {

}

@Nullable
@Override
protected EventCallback getEventCallback() {
return new EventCallback() {
@Override
public void onPlayerEvent(int eventCode, Bundle data) {
switch (eventCode){
case OnPlayerEventListener.PLAYER_EVENT_ON_PREPARED:
mHandler.sendEmptyMessage(MSG_LOAD_DANMU_DATA);
break;
case OnPlayerEventListener.PLAYER_EVENT_ON_SEEK_COMPLETE:
int seekTo = getPlayerStateGetter()!=null?getPlayerStateGetter().getCurrentPosition():-1;
mHandler.removeMessages(MSG_LOAD_DANMU_DATA);
Message obtain = Message.obtain();
obtain.what = MSG_LOAD_DANMU_DATA;
obtain.arg1 = seekTo;
mHandler.sendMessage(obtain);
break;
case OnPlayerEventListener.PLAYER_EVENT_ON_STOP:
case OnPlayerEventListener.PLAYER_EVENT_ON_RESET:
case OnPlayerEventListener.PLAYER_EVENT_ON_DESTROY:
case OnPlayerEventListener.PLAYER_EVENT_ON_DATA_SOURCE_SET:
mHandler.removeMessages(MSG_LOAD_DANMU_DATA);
break;
}
}
@Override
public void onErrorEvent(int eventCode, Bundle data) {
mHandler.removeMessages(MSG_LOAD_DANMU_DATA);
}
@Override
public void onReceiverEvent(int eventCode, Bundle data) {

}
};
}

}
7 changes: 7 additions & 0 deletions app/src/main/java/com/kk/taurus/avplayer/play/DataInter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ interface ReceiverKey{
String KEY_COMPLETE_COVER = "complete_cover";
String KEY_ERROR_COVER = "error_cover";
String KEY_CLOSE_COVER = "close_cover";
String KEY_DANMU_COVER = "danmu_cover";
}

interface PrivateEvent{
int EVENT_CODE_UPDATE_SEEK = -201;
}

interface ProducerEvent{

int ADD_DANMU_DATA = -301;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.kk.taurus.avplayer.App;
import com.kk.taurus.avplayer.R;
import com.kk.taurus.avplayer.cover.ControllerCover;
import com.kk.taurus.avplayer.cover.DanmuCover;
import com.kk.taurus.avplayer.event_producer.DanmuDataProducer;
import com.kk.taurus.avplayer.play.DataInter;
import com.kk.taurus.avplayer.play.ReceiverGroupManager;
import com.kk.taurus.avplayer.utils.DataUtils;
Expand Down Expand Up @@ -71,11 +73,16 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
updateVideo(false);

mReceiverGroup = ReceiverGroupManager.get().getReceiverGroup(this);
//添加弹幕cover
mReceiverGroup.addReceiver(DataInter.ReceiverKey.KEY_DANMU_COVER, new DanmuCover(this));
mReceiverGroup.getGroupValue().putBoolean(DataInter.Key.KEY_CONTROLLER_TOP_ENABLE, true);
mVideoView.setReceiverGroup(mReceiverGroup);
mVideoView.setEventHandler(onVideoViewEventHandler);
mVideoView.setOnPlayerEventListener(this);

//添加弹幕数据生产者
mVideoView.getSuperContainer().addEventProducer(new DanmuDataProducer());

mVideoView.setVolume(mVolumeLeft, mVolumeRight);
}

Expand Down
Loading

0 comments on commit e94c735

Please sign in to comment.