exoplayer实测播放dash流(二) dash+drm widevine加密流
程序员文章站
2022-07-14 17:22:39
...
一、前提
exoplayer基本的集成和用法参考上一篇:exoplayer的集成和使用一
国内对于播放流的版权还不是很重视,基本大多都是免费的。用到的dash加密情况不多,由于我这边服务的海外客户,所以研究了下。默认你有以下操作:
1.基本了解mediaplayer的使用、播放,以及对应的生命周期
2.能够自己集成exoplayer库。
3.服务端有认证地址,自己有所需要测试的dash加密流。
二、相关API的调用
2.1 客户端license获取流程:
代码如下:由于exoplayer源码过于臃肿,所以自己删减了下:
Application:
public class MyApplication extends Application {
protected String userAgent;
@Override
public void onCreate() {
super.onCreate();
userAgent = Util.getUserAgent(this, "ExoPlayer2018");
}
public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener)
{
return new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener));
}
public HttpDataSource.Factory buildHttpDataSourceFactory(TransferListener<? super DataSource> listener)
{
return new DefaultHttpDataSourceFactory(this.userAgent, listener);
}
}
Activty:
public class PlayerActivity extends Activity {
private String TAG="exo2018";
private DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();;
private DefaultDataSourceFactory defaultDataSourceFactory;
private Handler mHandler;
private PlayerView pv_view;
private DefaultTrackSelector trackSelector;
//认证地址
private String license_url="https://hk.cnv8.tv:9900/drm/index.php";
private SimpleExoPlayer mPlayer;
//无加密播放地址
private String url="https://hk.cnv8.tv:9900/dash/live/index.mpd";
private String url2="http://183.59.160.61:30001/PLTV/88888905/224/3221227518/index.m3u8";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
Log.i(TAG,"oncreate");
initView();
Intent intent = getIntent();
defaultDataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "PlayerDash_widevine"), (TransferListener) this.BANDWIDTH_METER);
mHandler = new Handler();
}
private void initView() {
Log.i(TAG,"initview");
pv_view = findViewById(R.id.pv_view2);
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG,"onstart");
initPlayer();
}
/**
* 初始化player
*/
private void initPlayer() {
TrackSelection.Factory trackSelectionFactory=new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
if (!"".equals(license_url))
try
{
DrmSessionManager localDrmSessionManager = buildDrmSessionManagerV18(C.WIDEVINE_UUID,license_url, null, false);
}
catch (UnsupportedDrmException localUnsupportedDrmException)
{
localUnsupportedDrmException.printStackTrace();
}
//工厂模式下新建一个player
mPlayer = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
//view绑定player
pv_view.setPlayer(mPlayer);
//自动播放
mPlayer.setPlayWhenReady(true);
DefaultDashChunkSource.Factory localFactory = new DefaultDashChunkSource.Factory(defaultDataSourceFactory);
//使用对应的库播放对应的流
DashMediaSource dashMediaSource = new DashMediaSource(Uri.parse(url), defaultDataSourceFactory, localFactory, null, null);
//进入prepare状态
mPlayer.prepare(dashMediaSource);
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG,"onresume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG,"onpause");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG,"ondestory");
releasePlayer();
}
/**
* 释放资源
*/
private void releasePlayer() {
if(mPlayer!=null){
mPlayer.release();
mPlayer=null;
trackSelector=null;
}
}
private DrmSessionManager<FrameworkMediaCrypto> buildDrmSessionManagerV18(UUID paramUUID, String paramString, String[] paramArrayOfString, boolean paramBoolean)
throws UnsupportedDrmException
{
HttpMediaDrmCallback localHttpMediaDrmCallback = new HttpMediaDrmCallback(paramString, buildHttpDataSourceFactory(false));
int i = 0;
if (paramArrayOfString != null)
while (i < -1 + paramArrayOfString.length)
{
localHttpMediaDrmCallback.setKeyRequestProperty(paramArrayOfString[i], paramArrayOfString[(i + 1)]);
i += 2;
}
DefaultDrmSessionManager localDefaultDrmSessionManager = new DefaultDrmSessionManager(paramUUID, FrameworkMediaDrm.newInstance(paramUUID), localHttpMediaDrmCallback, null,mHandler, new DefaultDrmSessionManager.EventListener()
{
public void onDrmKeysLoaded()
{
}
public void onDrmKeysRemoved()
{
}
public void onDrmKeysRestored()
{
}
public void onDrmSessionManagerError(Exception paramException)
{
}
});
return localDefaultDrmSessionManager;
}
private HttpDataSource.Factory buildHttpDataSourceFactory(boolean paramBoolean)
{
MyApplication localDemoApplication = (MyApplication)getApplication();
DefaultBandwidthMeter localDefaultBandwidthMeter;
if (paramBoolean)
localDefaultBandwidthMeter = BANDWIDTH_METER;
else
localDefaultBandwidthMeter = null;
return localDemoApplication.buildHttpDataSourceFactory(localDefaultBandwidthMeter);
}
}
源代码下载
上一篇: Linux之shell以及bash、dash的详解
下一篇: JavaScript覆盖率统计实现