實現播放視訊及彈幕
1:依賴: implementation ‘com.shuyu:GSYVideoPlayer:7.0.1’
implementation ‘com.github.ctiao:DanmakuFlameMaster:0.7.3’
2:佈局
<com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
android:id="@+id/video_player"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_centerInParent="true" />
<master.flame.danmaku.ui.widget.DanmakuView
android:id="@+id/danmaku_view"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_centerInParent="true" />
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/line"
android:layout_height="wrap_content">
<Button
android:id="@+id/butn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="傳送"></Button>
<EditText
android:id="@+id/ed"
android:layout_width="324dp"
android:layout_height="match_parent"></EditText>
</LinearLayout>
3:程式碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
OrientationUtils orientationUtils;
private DanmakuView danmaku_view;
private boolean showDanmaku;
private DanmakuContext danmakuContext;
/**
* 彈幕解析器
*/
private BaseDanmakuParser parser = new BaseDanmakuParser() {
@Override
protected IDanmakus parse() {
return new Danmakus();
}
};
private LinearLayout mOperationLayout;
private StandardGSYVideoPlayer video_player;
private Button butn1;
private EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
init();
initDanmaku();
}
private void init() {
video_player = (StandardGSYVideoPlayer) findViewById(R.id.video_player);
String source1 = “http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4”;
video_player.setUp(source1, true, “測試視訊”);
AudioManager service = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (service != null) {
service.setRingerMode(AudioManager.RINGER_MODE_SILENT);
service.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);
}
//增加封面
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.mipmap.ic_launcher);
video_player.setThumbImageView(imageView);
//增加title
video_player.getTitleTextView().setVisibility(View.VISIBLE);
//設定返回鍵
video_player.getBackButton().setVisibility(View.VISIBLE);
//設定旋轉
orientationUtils = new OrientationUtils(this, video_player);
//設定全屏按鍵功能,這是使用的是選擇螢幕,而不是全屏
video_player.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
orientationUtils.resolveByClick();
}
});
//是否可以滑動調整
video_player.setIsTouchWiget(true);
//設定返回按鍵功能
video_player.getBackButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
video_player.startPlayLogic();
}
@Override
protected void onPause() {
super.onPause();
video_player.onVideoPause();
if (danmaku_view != null && danmaku_view.isPrepared()) {
danmaku_view.pause();
}
}
@Override
protected void onResume() {
super.onResume();
video_player.onVideoResume();
if (danmaku_view != null && danmaku_view.isPrepared() && danmaku_view.isPaused()) {
danmaku_view.resume();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
GSYVideoManager.releaseAllVideos();
if (orientationUtils != null)
orientationUtils.releaseListener();
showDanmaku = false;
if (danmaku_view != null) {
danmaku_view.release();
danmaku_view = null;
}
}
@Override
public void onBackPressed() {
//先返回正常狀態
if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
video_player.getFullscreenButton().performClick();
return;
}
//釋放所有
video_player.setVideoAllCallBack(null);
super.onBackPressed();
}
private void initView() {
danmaku_view = (DanmakuView) findViewById(R.id.danmaku_view);
video_player = (StandardGSYVideoPlayer) findViewById(R.id.video_player);
butn1 = (Button) findViewById(R.id.butn1);
mOperationLayout = (LinearLayout) findViewById(R.id.line);
butn1.setOnClickListener(this);
ed = (EditText) findViewById(R.id.ed);
//監聽由於輸入法彈出所致的沉浸問題
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if (visibility == View.SYSTEM_UI_FLAG_VISIBLE) {
onWindowFocusChanged(true);
}
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.butn1:
String data = ed.getText().toString();
if (!TextUtils.isEmpty(data)) {
addDanmaku(data, true);
ed.setText("");
}
break;
}
}
/**
* sp轉px的方法。
*/
public int sp2px(float spValue) {
final float fontScale = getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
private void addDanmaku(String data, boolean b) {
//彈幕例項BaseDanmaku,傳入引數是彈幕方向
BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
danmaku.text = data;
danmaku.padding = 5;
danmaku.textSize = sp2px(20);
danmaku.textColor = Color.WHITE;
danmaku.setTime(danmaku_view.getCurrentTime());
//加邊框
if (b) {
danmaku.borderColor = Color.GREEN;
}
danmaku_view.addDanmaku(danmaku);
}
/**
* 初始化彈幕元件
*/
private void initDanmaku() {
//給彈幕檢視設定回撥,在準備階段獲取彈幕資訊並開始
danmaku_view.setCallback(new DrawHandler.Callback() {
@Override
public void prepared() {
showDanmaku = true;
danmaku_view.start();
generateSomeDanmaku();
}
@Override
public void updateTimer(DanmakuTimer timer) {
}
@Override
public void danmakuShown(BaseDanmaku danmaku) {
}
@Override
public void drawingFinished() {
}
});
//快取,提升繪製效率
danmaku_view.enableDanmakuDrawingCache(true);
//DanmakuContext主要用於彈幕樣式的設定
danmakuContext = DanmakuContext.create();
danmakuContext.setDanmakuStyle(IDisplayer.DANMAKU_STYLE_STROKEN, 3);//描邊
danmakuContext.setDuplicateMergingEnabled(true);//重複合併
danmakuContext.setScrollSpeedFactor(1.2f);//彈幕滾動速度
//讓彈幕進入準備狀態,傳入彈幕解析器和樣式設定
danmaku_view.prepare(parser, danmakuContext);
}
private void generateSomeDanmaku() {
new Thread(new Runnable() {
@Override
public void run() {
while (showDanmaku) {
int time = new Random().nextInt(300);
String content = "" + time + time;
addDanmaku(content, false);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}
相關文章
- 用canvas實現視訊播放與彈幕功能Canvas
- 使用Dplayer實現Vue3中的視訊及彈幕播放Vue
- Dplayer Html5 彈幕視訊播放器的實現HTML播放器
- web前端培訓分享:使用Dplayer實現Vue3中的視訊及彈幕播放Web前端Vue
- 使用 mask 實現視訊彈幕人物遮罩過濾遮罩
- 視訊彈幕外掛
- html5 canvas實現高併發視訊彈幕功能HTMLCanvas
- Flutter 實現視訊全屏播放邏輯及解析Flutter
- java 視訊播放 多人及時彈幕技術 程式碼生成器 websocket springmvc mybatis SSMJavaWebSpringMVCMyBatisSSM
- HTML5本地視訊播放器設計(+彈幕功能)HTML播放器
- 使用danmuplayer.js外掛實現Java web網站視訊發彈幕JSJavaWeb網站
- ? 鬥魚彈幕php實現PHP
- 帶你用AVPlayer實現音訊和視訊播放音訊
- 短視訊系統原始碼,直播間實現彈幕的自動傳送原始碼
- video自定義實現視訊播放功能IDE
- 如何實現視訊加密全平臺播放加密
- 「Premiere中文新手教程」視訊彈幕製作REM
- 螢石雲視訊EZOpenSDK整合(實現播放,視訊放大功能)
- Android彈幕功能實現,模仿鬥魚直播的彈幕效果Android
- Android中SurfaceView視訊播放實現程式碼AndroidView
- 35.FFmpeg+OpenGLES+OpenSLES播放器實現(九.OpenGLES播放視訊)播放器
- 使用vue控制video視訊和彈幕功能VueIDE
- Go使用websocket實現彈幕功能GoWeb
- Flutter 實現虎牙/鬥魚 彈幕效果Flutter
- 自定義簡單彈幕實現
- ffmpeg播放器實現詳解 - 視訊顯示播放器
- Electron實現跨平臺全能視訊播放器播放器
- 彈幕播放器【在專案中對彈幕播放器一些零碎的整理】播放器
- vue+樹莓派實現實時視訊播放暫停Vue樹莓派
- 基於swift4.0實現視訊播放、螢幕旋轉、倍速播放、手勢調節,鎖屏皮膚等功能Swift
- sync-player:使用websocket實現異地同步播放視訊Web
- vue pc端實現 直播功能+彈幕Vue
- RecyclerView實現的彈幕之中文DOCView
- iOS彈幕高效載入實現方式iOS
- 無線端的彈幕實現方案
- 【html5多媒體】自定義播放器(彈幕,水印,甚至在視訊開頭新增封面)HTML播放器
- DM6446的音訊編解碼及播放實現音訊
- 視音訊播放音訊