一、exoplayer的相关库的集成关联
From JCenter
本人的是as3.1。我这里的exo库用的是2.8.0。取消其他版本的点这里:历代exoplayer-release版本简介。
git clone https://github.com/google/ExoPlayer.git git checkout release-v2
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')
implementation project(':exoplayer-library-core') implementation project(':exoplayer-library-dash') implementation project(':exoplayer-library-ui')
二、exoplayer的使用。
<?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>
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; } } }
@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; } }
效果图: