查了很多东西,终于发现一个非常不错的第三方框架分享给大家,直接写用法

1.添加类库

gradle

dependencies {

# 必选,内部默认使用系统mediaplayer进行解码

implementation 'com.github.dueeeke.dkplayer:dkplayer-java:3.2.6'

# 可选,包含StandardVideoController的实现

implementation 'com.github.dueeeke.dkplayer:dkplayer-ui:3.2.6'

# 可选,使用exoplayer进行解码

implementation 'com.github.dueeeke.dkplayer:player-exo:3.2.6'

# 可选,使用ijkplayer进行解码

implementation 'com.github.dueeeke.dkplayer:player-ijk:3.2.6'

# 可选,如需要缓存或者抖音预加载功能请引入此库

implementation 'com.github.dueeeke.dkplayer:videocache:3.2.6'

}

或者将library下载并导入项目中使用

2.添加布局

android:id="@+id/player"

android:layout_width="match_parent"

android:layout_height="300dp" />

注意:一定要定死宽高

3.设置视频地址、控制器等

videoView.setUrl(URL_VOD); //设置视频地址

StandardVideoController controller = new StandardVideoController(this);

controller.addDefaultControlComponent("标题", false);

videoView.setVideoController(controller); //设置控制器

videoView.start(); //开始播放,不调用则不自动播放

4.在Activity中

@Override

protected void onPause() {

super.onPause();

videoView.pause();

}

@Override

protected void onResume() {

super.onResume();

videoView.resume();

}

@Override

protected void onDestroy() {

super.onDestroy();

videoView.release();

}

@Override

public void onBackPressed() {

if (!videoView.onBackPressed()) {

super.onBackPressed();

}

}

5.在AndroidManifest.xml中

android:name=".PlayerActivity"

android:configChanges="orientation|screenSize|keyboardHidden"

android:screenOrientation="portrait" />

6. 下面是在项目中的代码

private void setUrl() {

try{

//使用IjkPlayer解码

OCFile file = getIntent().getParcelableExtra("file");

Account mAccount = getIntent().getParcelableExtra("account");

OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, YHLMyVideoActivity.this);

OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().

getClientFor(ocAccount, YHLMyVideoActivity.this);

String url = client.getBaseUri()+"/remote.php/webdav"+file.getRemotePath();

String userAgent = OwnCloudClientManagerFactory.getUserAgent();

Map headers = new HashMap<>();

String authorization = getSharedPreferences("hptk", Context.MODE_PRIVATE)

.getString("Authorization", "");

headers.put("Authorization", authorization);

headers.put(OCS_API_HEADER, OCS_API_HEADER_VALUE);

headers.put("User-Agent", userAgent);

Log_OC.d("aaa","User-Agent = " + userAgent);

Log_OC.d("aaa","authorization = " + authorization);

Log_OC.d("aaa","url = " + url);

//-------------

mVideoView.setPlayerFactory(IjkPlayerFactory.create());

StandardVideoController controller = new StandardVideoController(this);

//根据屏幕方向自动进入/退出全屏

controller.setEnableOrientation(true);

PrepareView prepareView = new PrepareView(this);//准备播放界面

controller.addControlComponent(prepareView);

controller.addControlComponent(new CompleteView(this));//自动完成播放界面

controller.addControlComponent(new ErrorView(this));//错误界面

TitleView titleView = new TitleView(this);//标题栏

controller.addControlComponent(titleView);

VodControlView vodControlView = new VodControlView(this);//点播控制条

//是否显示底部进度条。默认显示

vodControlView.showBottomProgress(true);

controller.addControlComponent(vodControlView);

GestureView gestureControlView = new GestureView(this);//滑动控制视图

controller.addControlComponent(gestureControlView);

//根据是否为直播决定是否需要滑动调节进度

controller.setCanChangePosition(true);

//设置标题

titleView.setTitle("");

mVideoView.setVideoController(controller);

mVideoView.setUrl(url,headers);

//播放状态监听

mVideoView.addOnStateChangeListener(mOnStateChangeListener);

mVideoView.start();

}catch (Exception e){

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐