Android https 视频,android 播放https 并且可以设置headers

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

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值