Android實(shí)現(xiàn)視頻彈幕功能
本文實(shí)例為大家分享了Android視頻彈幕的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
上圖:代碼隨機(jī)生成的彈幕及彈幕輸入欄
下圖:綠色框的彈幕為用戶手動(dòng)添加發(fā)送的彈幕
1.準(zhǔn)備工作
準(zhǔn)備一個(gè)視頻文件,將該視頻文件放到res/raw目錄下。
需要將視頻設(shè)置為橫屏 ,即往配置文件中添加android:screenOrientation="landscape":
<activity android:name=".MainActivity" android:configChanges="keyboardHidden|orientation|screenLayout|screenSize" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
這里用到了嗶哩嗶哩開(kāi)源的彈幕效果庫(kù)DanmakuFlameMaster,需要配置到模塊的build.gradle的dependencies中
注:DanmakuFlameMaster的版本最好使用在0.9以上,否則會(huì)存在一些彈幕bug
2.布局
使用一個(gè)相對(duì)布局,彈幕浮于視頻之上,底部是彈幕文字輸入欄,右下角為彈幕發(fā)送按鈕:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true"/> <master.flame.danmaku.ui.widget.DanmakuView android:id="@+id/danmu" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/operate_layout" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:visibility="gone"> <EditText android:id="@+id/edit" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginLeft="50dp" android:textColor="#ffffff" android:imeOptions="flagNoExtractUi" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/send" android:textSize="20sp" android:background="#00000000" android:textColor="#ffffff" android:text="發(fā)送"/> </LinearLayout> </RelativeLayout>
3.視頻彈幕的實(shí)現(xiàn)
<1> 視頻使用VideoView來(lái)進(jìn)行 ;
<2>關(guān)于彈幕庫(kù)的使用,需要?jiǎng)?chuàng)建一個(gè)DanmakuContext的實(shí)例和一個(gè)彈幕的解析器(這里直接創(chuàng)建了一個(gè)全局的BaseDanmakuParser),創(chuàng)建完成后就可以調(diào)用DanmakuView的prepare()方法了,調(diào)用這一方法后會(huì)自動(dòng)調(diào)用回調(diào)函數(shù)中的prepared()方法,在這個(gè)方法中調(diào)用了start方法,彈幕就此開(kāi)始工作了;
<3>需要在onPause()、onResume()、onDestroy()方法中執(zhí)行一些操作,以保證DanmakuView的資源可以得到釋放。
下面附上完整代碼
package com.mega.mydanmudemo; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.VideoView; import java.util.Random; import master.flame.danmaku.controller.DrawHandler; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.DanmakuTimer; import master.flame.danmaku.danmaku.model.IDanmakus; import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.ui.widget.DanmakuView; public class MainActivity extends AppCompatActivity { private boolean showDanma; private VideoView mVideoView; private DanmakuView mDanmu; private BaseDanmakuParser mBaseDanmakuParser = new BaseDanmakuParser() {//彈幕解析器 @Override protected IDanmakus parse() { return new Danmakus(); } }; private DanmakuContext danmakuContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); setOnListener(); } /*** * 一些初始化工作 */ private void init() { String uri = "android.resource://" + getPackageName() + "/" + R.raw.danmu; mVideoView = (VideoView)findViewById(R.id.video_view); mVideoView.setVideoPath(uri); mVideoView.start(); mDanmu = (DanmakuView)findViewById(R.id.danmu); mDanmu.enableDanmakuDrawingCache(true); danmakuContext = DanmakuContext.create(); danmakuContext.setScaleTextSize(1.1f); mDanmu.prepare(mBaseDanmakuParser,danmakuContext); } /*** * 彈幕的準(zhǔn)備工作,發(fā)送按鈕監(jiān)聽(tīng)。。 */ private void setOnListener(){ mDanmu.setCallback(new DrawHandler.Callback() { @Override public void prepared() { showDanma = true; mDanmu.start();//啟動(dòng)彈幕 generateSomeDanmu(); } @Override public void updateTimer(DanmakuTimer timer) { } @Override public void danmakuShown(BaseDanmaku danmaku) { } @Override public void drawingFinished() { } }); final LinearLayout operate_view = (LinearLayout)findViewById(R.id.operate_layout); Button send = (Button)findViewById(R.id.send); final EditText editText = (EditText)findViewById(R.id.edit); mDanmu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (operate_view.getVisibility() == View.GONE){ operate_view.setVisibility(View.VISIBLE); }else{ operate_view.setVisibility(View.GONE); } } }); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String content = editText.getText().toString(); if (!TextUtils.isEmpty(content)){ InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(),0); addDamu(content,true); editText.setText(""); operate_view.setVisibility(View.GONE); } } }); getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { if (visibility == View.SYSTEM_UI_FLAG_VISIBLE){ onWindowFocusChanged(true); } } }); } /*** * 隨機(jī)產(chǎn)生一些彈幕 */ private void generateSomeDanmu() { new Thread(new Runnable() { @Override public void run() { while (showDanma){ int time = new Random().nextInt(300); String content = "" + time; addDamu(content,false); try { Thread.sleep(time); }catch (Exception e){ e.printStackTrace(); } } } }).start(); } /*** * 添加彈幕的方法 * @param content 彈幕的內(nèi)容 * @param isSelf 是否是用戶發(fā)送的彈幕 */ private void addDamu(String content,boolean isSelf) { BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL); danmaku.text = content; danmaku.padding = 5; danmaku.priority = 0; danmaku.textSize = sp2px(20); danmaku.setTime(mDanmu.getCurrentTime()); danmaku.textColor = Color.argb(new Random().nextInt(256), new Random().nextInt(256), new Random().nextInt(256),new Random().nextInt(256)); if (isSelf){ danmaku.borderColor = Color.GREEN; } mDanmu.addDanmaku(danmaku); } private float sp2px(int i) { final float fontScale = getResources().getDisplayMetrics().scaledDensity; return (int)(i* fontScale +0.5f); } @Override protected void onPause() { super.onPause(); if (mDanmu!= null && mDanmu.isPrepared()){ mDanmu.pause(); } } @Override protected void onResume() { super.onResume(); if (mDanmu!=null&& mDanmu.isPrepared()&& mDanmu.isPaused()){ mDanmu.resume(); } } @Override protected void onDestroy() { super.onDestroy(); showDanma = false; if (mDanmu != null){ mDanmu.release(); mDanmu = null; } } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus && Build.VERSION.SDK_INT>=19){ View deview = getWindow().getDecorView(); deview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |View.SYSTEM_UI_FLAG_FULLSCREEN |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:SurfaceView 視頻發(fā)送彈幕并實(shí)現(xiàn)滾動(dòng)歌詞
欄 目:Android
下一篇:詳解關(guān)于AndroidQ獲取不到imsi解決方案
本文標(biāo)題:Android實(shí)現(xiàn)視頻彈幕功能
本文地址:http://mengdiqiu.com.cn/a1/Android/9042.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery