【移动应用开发技术】Android如何实现视频弹幕功能_第1页
【移动应用开发技术】Android如何实现视频弹幕功能_第2页
【移动应用开发技术】Android如何实现视频弹幕功能_第3页
【移动应用开发技术】Android如何实现视频弹幕功能_第4页
【移动应用开发技术】Android如何实现视频弹幕功能_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】Android如何实现视频弹幕功能

这篇文章给大家分享的是有关Android如何实现视频弹幕功能的内容。在下觉得挺实用的,因此分享给大家做个参考,一起跟随在下过来看看吧。具体内容如下效果图:上图:代码随机生成的弹幕及弹幕输入栏下图:绿色框的弹幕为用户手动添加发送的弹幕1.准备工作准备一个视频文件,将该视频文件放到res/raw目录下。需要将视频设置为横屏播放,即往配置文件中添加android:screenOrientation="landscape":<activity

android:name=".MainActivity"

android:configChanges="keyboardHidden|orientation|screenLayout|screenSize"

android:screenOrientation="landscape">

<intent-filter>

<action

android:name="ent.action.MAIN"

/>

<category

android:name="ent.category.LAUNCHER"

/>

</intent-filter>

</activity>这里用到了哔哩哔哩开源的弹幕效果库DanmakuFlameMaster,需要配置到模块的build.gradle的dependencies中注:DanmakuFlameMaster的版本最好使用在0.9以上,否则会存在一些弹幕bug2.布局使用一个相对布局,弹幕浮于视频之上,底部是弹幕文字输入栏,右下角为弹幕发送按钮:<?xml

version="1.0"

encoding="utf-8"?>

<RelativeLayout

xmlns:android="/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="发送"/>

</LinearLayout>

</RelativeLayout>3.视频弹幕的实现<1>播放视频使用VideoView来进行播放;<2>关于弹幕库的使用,需要创建一个DanmakuContext的实例和一个弹幕的解析器(这里直接创建了一个全局的BaseDanmakuParser),创建完成后就可以调用DanmakuView的prepare()方法了,调用这一方法后会自动调用回调函数中的prepared()方法,在这个方法中调用了start方法,弹幕就此开始工作了;<3>需要在onPause()、onResume()、onDestroy()方法中执行一些操作,以保证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);

}

/***

*

弹幕的准备工作,发送按钮监听。。

*/

private

void

setOnListener(){

mDanmu.setCallback(new

DrawHandler.Callback()

{

@Override

public

void

prepared()

{

showDanma

=

true;

mDanmu.start();//启动弹幕

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);

}

}

});

}

/***

*

随机产生一些弹幕

*/

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

弹幕的内容

*

@param

isSelf

是否是用户发送的弹幕

*/

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.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论