一、exoplayer的相关库的集成关联
1.1 关联的方法有两种,第一种是常规的远程依赖
From JCenter
本人的是as3.1。我这里的exo库用的是2.8.0。需要使用其他版本的点这里:历代exoplayer-release版本简介。
1.2第二种关联。是将工程下载到本地,然后添加本地库。
1.2.1下载到本地:
使用git命令下载到本地,或者直接在github上download下来
git clone https://github.com/google/ExoPlayer.git
git checkout release-v2
1.2.1修改settings.gradle这个文件,替代成你下载到的路径,如下所示:
我将exoplayer下载到了e:/exo player/ 在自己新建的的项目里面的setting.gradle文件 里面修改:
include ':app', ':library', ':library-dash', ':lib100'
gradle.ext.exoplayerRoot = 'E:\\exo player\\ExoPlayer-release-v2'
gradle.ext.exoplayerModulePrefix = 'exoplayer-'
apply from: new File(gradle.ext.exoplayerRoot, 'core_settings.gradle')
这样就生成本地的库了
1.2.3 本地关联
implementation project(':exoplayer-library-core')
implementation project(':exoplayer-library-dash')
implementation project(':exoplayer-library-ui')
二、exoplayer的使用。
2.1 布局文件activity_main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/pv_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
2.2 创建exoplayer对象,及简单播放dash流
public class MainActivity extends AppCompatActivity {
private PlayerView pv_view;
private DataSource.Factory mediaDataSourceFactory;
private SimpleExoPlayer mPlayer;
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private MediaSource dashMediaSource;
private DefaultTrackSelector trackSelector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mediaDataSourceFactory = buildDataSourceFactory(true);
initMediaPlayer();
}
/**
* 初始化mediaplayer
*/
private void initMediaPlayer() {
//dash测试流
Uri mUri = Uri.parse("https://hk.cnv8.tv:9900/dash/vod/dash.mpd");
//CCTV12
Uri mUri2 = Uri.parse("http://183.59.160.61:30001/PLTV/88888905/224/3221227483/index.m3u8");
//电信内网CCTV1
Uri mUri3 = Uri.parse("http://183.59.160.61:30001/PLTV/88888905/224/3221227518/index.m3u8");
TrackSelection.Factory trackSelectionFactory=new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
DefaultTrackSelector.Parameters trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
trackSelector.setParameters(trackSelectorParameters);
mPlayer= ExoPlayerFactory.newSimpleInstance(this, trackSelector);
pv_view.setPlayer(mPlayer);
//使用dash的解析库
dashMediaSource = new DashMediaSource(mUri,mediaDataSourceFactory, new DefaultDashChunkSource.Factory(mediaDataSourceFactory
),null,null);
//使用hls解析库
HlsMediaSource hlsMediaSource = new HlsMediaSource(mUri3, mediaDataSourceFactory, null, null);
mPlayer.prepare(hlsMediaSource);
mPlayer.setPlayWhenReady(true); //自动播放
}
private void initView() {
pv_view= findViewById(R.id.pv_view);
}
private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
return ((MyApplication) getApplication())
.buildDataSourceFactory(useBandwidthMeter ? null : null);
}
@Override
protected void onStop() {
super.onStop();
releasePlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
}
/**
* 释放资源
*/
private void releasePlayer() {
if(mPlayer!=null){
mPlayer.release();
mPlayer = null;
dashMediaSource = null;
trackSelector = null;
}
}
}
2.3player的资源释放:
@Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
}
/**
* 释放资源
*/
private void releasePlayer() {
if(mPlayer!=null){
mPlayer.release();
mPlayer = null;
dashMediaSource = null;
trackSelector = null;
}
}
2.4 manifest权限添加
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
其他:值得注意的是:创建的有些方法是在application里面的:
public class MyApplication extends Application {
protected String userAgent;
private Cache downloadCache;
private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
private File downloadDirectory;
@Override
public void onCreate() {
super.onCreate();
userAgent = Util.getUserAgent(this, "ExoPlayer2018");
}
/** Returns a {@link DataSource.Factory}. */
public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener) {
DefaultDataSourceFactory upstreamFactory =
new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener));
return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache());
}
/** Returns a {@link HttpDataSource.Factory}. */
public HttpDataSource.Factory buildHttpDataSourceFactory(
TransferListener<? super DataSource> listener) {
return new DefaultHttpDataSourceFactory(userAgent, listener);
}
private synchronized Cache getDownloadCache() {
if (downloadCache == null) {
File downloadContentDirectory = new File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY);
downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor());
}
return downloadCache;
}
private static CacheDataSourceFactory buildReadOnlyCacheDataSource(
DefaultDataSourceFactory upstreamFactory, Cache cache) {
return new CacheDataSourceFactory(
cache,
upstreamFactory,
new FileDataSourceFactory(),
/* cacheWriteDataSinkFactory= */ null,
CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,
/* eventListener= */ null);
}
private File getDownloadDirectory() {
if (downloadDirectory == null) {
downloadDirectory = getExternalFilesDir(null);
if (downloadDirectory == null) {
downloadDirectory = getFilesDir();
}
}
return downloadDirectory;
}
}
效果图:
其他资料: