|
| 1 | +package com.kk.taurus.avplayer.cover; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Color; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | + |
| 9 | +import com.kk.taurus.avplayer.R; |
| 10 | +import com.kk.taurus.avplayer.play.DataInter; |
| 11 | +import com.kk.taurus.playerbase.event.EventKey; |
| 12 | +import com.kk.taurus.playerbase.event.OnPlayerEventListener; |
| 13 | +import com.kk.taurus.playerbase.log.PLog; |
| 14 | +import com.kk.taurus.playerbase.receiver.BaseCover; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | + |
| 18 | +import master.flame.danmaku.controller.DrawHandler; |
| 19 | +import master.flame.danmaku.controller.IDanmakuView; |
| 20 | +import master.flame.danmaku.danmaku.model.BaseDanmaku; |
| 21 | +import master.flame.danmaku.danmaku.model.DanmakuTimer; |
| 22 | +import master.flame.danmaku.danmaku.model.IDisplayer; |
| 23 | +import master.flame.danmaku.danmaku.model.android.DanmakuContext; |
| 24 | +import master.flame.danmaku.danmaku.model.android.Danmakus; |
| 25 | +import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; |
| 26 | + |
| 27 | +/** |
| 28 | + * @ClassName DanmuCover |
| 29 | + * @Description |
| 30 | + * @Author Taurus |
| 31 | + * @Date 2020/9/6 7:12 PM |
| 32 | + */ |
| 33 | +public class DanmuCover extends BaseCover { |
| 34 | + |
| 35 | + private IDanmakuView mDanmakuView; |
| 36 | + private DanmakuContext mDanmakuContext; |
| 37 | + |
| 38 | + public DanmuCover(Context context) { |
| 39 | + super(context); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onReceiverBind() { |
| 44 | + super.onReceiverBind(); |
| 45 | + |
| 46 | + mDanmakuView = findViewById(R.id.layout_danmu_cover_danmu_view); |
| 47 | + mDanmakuView.setCallback(new DrawHandler.Callback() { |
| 48 | + @Override |
| 49 | + public void prepared() { |
| 50 | + mDanmakuView.start(); |
| 51 | + } |
| 52 | + @Override |
| 53 | + public void updateTimer(DanmakuTimer timer) { |
| 54 | + |
| 55 | + } |
| 56 | + @Override |
| 57 | + public void danmakuShown(BaseDanmaku danmaku) { |
| 58 | + |
| 59 | + } |
| 60 | + @Override |
| 61 | + public void drawingFinished() { |
| 62 | + |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + HashMap<Integer, Integer> maxLinesPair = new HashMap<Integer, Integer>(); |
| 67 | + maxLinesPair.put(BaseDanmaku.TYPE_SCROLL_RL, 5); // 滚动弹幕最大显示5行 |
| 68 | + // 设置是否禁止重叠 |
| 69 | + HashMap<Integer, Boolean> overlappingEnablePair = new HashMap<Integer, Boolean>(); |
| 70 | + overlappingEnablePair.put(BaseDanmaku.TYPE_SCROLL_RL, true); |
| 71 | + overlappingEnablePair.put(BaseDanmaku.TYPE_FIX_TOP, true); |
| 72 | + mDanmakuContext = DanmakuContext.create(); |
| 73 | + mDanmakuContext.setDanmakuStyle(IDisplayer.DANMAKU_STYLE_STROKEN, 3).setDuplicateMergingEnabled(false).setScrollSpeedFactor(1.2f).setScaleTextSize(1.2f) |
| 74 | + .setMaximumLines(maxLinesPair) |
| 75 | + .preventOverlapping(overlappingEnablePair).setDanmakuMargin(40); |
| 76 | + |
| 77 | + mDanmakuView.prepare(new BaseDanmakuParser() { |
| 78 | + @Override |
| 79 | + protected Danmakus parse() { |
| 80 | + return new Danmakus(); |
| 81 | + } |
| 82 | + }, mDanmakuContext); |
| 83 | + mDanmakuView.showFPS(true); |
| 84 | + mDanmakuView.enableDanmakuDrawingCache(true); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected View onCreateCoverView(Context context) { |
| 89 | + return LayoutInflater.from(context).inflate(R.layout.layout_danmu_cover, null, false); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public int getCoverLevel() { |
| 94 | + return levelLow(1); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onPlayerEvent(int eventCode, Bundle bundle) { |
| 99 | + switch (eventCode){ |
| 100 | + case OnPlayerEventListener.PLAYER_EVENT_ON_PAUSE: |
| 101 | + if (mDanmakuView != null && mDanmakuView.isPrepared()) { |
| 102 | + mDanmakuView.pause(); |
| 103 | + } |
| 104 | + break; |
| 105 | + case OnPlayerEventListener.PLAYER_EVENT_ON_RESUME: |
| 106 | + if (mDanmakuView != null && mDanmakuView.isPrepared() && mDanmakuView.isPaused()) { |
| 107 | + mDanmakuView.resume(); |
| 108 | + } |
| 109 | + break; |
| 110 | + case OnPlayerEventListener.PLAYER_EVENT_ON_DESTROY: |
| 111 | + if (mDanmakuView != null) { |
| 112 | + // dont forget release! |
| 113 | + mDanmakuView.release(); |
| 114 | + mDanmakuView = null; |
| 115 | + } |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onErrorEvent(int eventCode, Bundle bundle) { |
| 122 | + if (mDanmakuView != null && mDanmakuView.isPrepared() && mDanmakuView.isPaused()) { |
| 123 | + mDanmakuView.resume(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onReceiverEvent(int eventCode, Bundle bundle) { |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onProducerEvent(int eventCode, Bundle bundle) { |
| 134 | + super.onProducerEvent(eventCode, bundle); |
| 135 | + if(eventCode == DataInter.ProducerEvent.ADD_DANMU_DATA){ |
| 136 | + PLog.d("DanmuTest_Receive", bundle.toString()); |
| 137 | + int position = bundle.getInt(EventKey.INT_DATA); |
| 138 | + String text = bundle.getString(EventKey.STRING_DATA); |
| 139 | + addDanmaku(false, position, text); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void addDanmaku(boolean islive, int position, String text) { |
| 144 | + if(mDanmakuContext==null) |
| 145 | + return; |
| 146 | + BaseDanmaku danmaku = mDanmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL); |
| 147 | + if (danmaku == null || mDanmakuView == null) { |
| 148 | + return; |
| 149 | + } |
| 150 | + // for(int i=0;i<100;i++){ |
| 151 | + // } |
| 152 | + danmaku.text = text; |
| 153 | + danmaku.padding = 5; |
| 154 | + danmaku.priority = 0; // 可能会被各种过滤器过滤并隐藏显示 |
| 155 | + danmaku.isLive = islive; |
| 156 | + danmaku.setTime(position + 1200); |
| 157 | + danmaku.textSize = 25f * (getContext().getResources().getDisplayMetrics().density - 0.6f); |
| 158 | + danmaku.textColor = Color.RED; |
| 159 | + danmaku.textShadowColor = Color.WHITE; |
| 160 | + // danmaku.underlineColor = Color.GREEN; |
| 161 | + danmaku.borderColor = Color.GREEN; |
| 162 | + mDanmakuView.addDanmaku(danmaku); |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments