当前位置:   article > 正文

exoplayer的使用简介和实测播放dash流、hls(一)_exo如何播放hls

exo如何播放hls

一、exoplayer的相关库的集成关联

     1.1   关联的方法有两种,第一种是常规的远程依赖

From JCenter   


本人的是as3.1。我这里的exo库用的是2.8.0。需要使用其他版本的点这里:历代exoplayer-release版本简介

     

   1.2第二种关联。是将工程下载到本地,然后添加本地库。

     1.2.1下载到本地:

   使用git命令下载到本地,或者直接在github上download下来

  1. git clone https://github.com/google/ExoPlayer.git
  2. git checkout release-v2

    1.2.1修改settings.gradle这个文件,替代成你下载到的路径,如下所示:

 我将exoplayer下载到了e:/exo player/   在自己新建的的项目里面的setting.gradle文件 里面修改: 

  1. include ':app', ':library', ':library-dash', ':lib100'
  2. gradle.ext.exoplayerRoot = 'E:\\exo player\\ExoPlayer-release-v2'
  3. gradle.ext.exoplayerModulePrefix = 'exoplayer-'
  4. apply from: new File(gradle.ext.exoplayerRoot, 'core_settings.gradle')

  这样就生成本地的库了

 1.2.3 本地关联

  
  1. implementation project(':exoplayer-library-core')
  2. implementation project(':exoplayer-library-dash')
  3. implementation project(':exoplayer-library-ui')


二、exoplayer的使用。

   2.1 布局文件activity_main:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <com.google.android.exoplayer2.ui.PlayerView
  10. android:id="@+id/pv_view"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"/>
  13. </android.support.constraint.ConstraintLayout>

   2.2 创建exoplayer对象,及简单播放dash流

   
  1. public class MainActivity extends AppCompatActivity {
  2. private PlayerView pv_view;
  3. private DataSource.Factory mediaDataSourceFactory;
  4. private SimpleExoPlayer mPlayer;
  5. private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
  6. private MediaSource dashMediaSource;
  7. private DefaultTrackSelector trackSelector;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. initView();
  13. mediaDataSourceFactory = buildDataSourceFactory(true);
  14. initMediaPlayer();
  15. }
  16. /**
  17. * 初始化mediaplayer
  18. */
  19. private void initMediaPlayer() {
  20. //dash测试流
  21. Uri mUri = Uri.parse("https://hk.cnv8.tv:9900/dash/vod/dash.mpd");
  22. //CCTV12
  23. Uri mUri2 = Uri.parse("http://183.59.160.61:30001/PLTV/88888905/224/3221227483/index.m3u8");
  24. //电信内网CCTV1
  25. Uri mUri3 = Uri.parse("http://183.59.160.61:30001/PLTV/88888905/224/3221227518/index.m3u8");
  26. TrackSelection.Factory trackSelectionFactory=new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
  27. DefaultTrackSelector.Parameters trackSelectorParameters = new DefaultTrackSelector.ParametersBuilder().build();
  28. trackSelector = new DefaultTrackSelector(trackSelectionFactory);
  29. trackSelector.setParameters(trackSelectorParameters);
  30. mPlayer= ExoPlayerFactory.newSimpleInstance(this, trackSelector);
  31. pv_view.setPlayer(mPlayer);
  32. //使用dash的解析库
  33. dashMediaSource = new DashMediaSource(mUri,mediaDataSourceFactory, new DefaultDashChunkSource.Factory(mediaDataSourceFactory
  34. ),null,null);
  35. //使用hls解析库
  36. HlsMediaSource hlsMediaSource = new HlsMediaSource(mUri3, mediaDataSourceFactory, null, null);
  37. mPlayer.prepare(hlsMediaSource);
  38. mPlayer.setPlayWhenReady(true); //自动播放
  39. }
  40. private void initView() {
  41. pv_view= findViewById(R.id.pv_view);
  42. }
  43. private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
  44. return ((MyApplication) getApplication())
  45. .buildDataSourceFactory(useBandwidthMeter ? null : null);
  46. }
  47. @Override
  48. protected void onStop() {
  49. super.onStop();
  50. releasePlayer();
  51. }
  52. @Override
  53. protected void onDestroy() {
  54. super.onDestroy();
  55. releasePlayer();
  56. }
  57. /**
  58. * 释放资源
  59. */
  60. private void releasePlayer() {
  61. if(mPlayer!=null){
  62. mPlayer.release();
  63. mPlayer = null;
  64. dashMediaSource = null;
  65. trackSelector = null;
  66. }
  67. }
  68. }



   2.3player的资源释放:

  1. @Override
  2. protected void onDestroy() {
  3. super.onDestroy();
  4. releasePlayer();
  5. }
  1. /**
  2. * 释放资源
  3. */
  4. private void releasePlayer() {
  5. if(mPlayer!=null){
  6. mPlayer.release();
  7. mPlayer = null;
  8. dashMediaSource = null;
  9. trackSelector = null;
  10. }
  11. }

2.4 manifest权限添加

  1. <uses-permission android:name="android.permission.INTERNET"/>
  2. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

其他:值得注意的是:创建的有些方法是在application里面的:

  1. public class MyApplication extends Application {
  2. protected String userAgent;
  3. private Cache downloadCache;
  4. private static final String DOWNLOAD_CONTENT_DIRECTORY = "downloads";
  5. private File downloadDirectory;
  6. @Override
  7. public void onCreate() {
  8. super.onCreate();
  9. userAgent = Util.getUserAgent(this, "ExoPlayer2018");
  10. }
  11. /** Returns a {@link DataSource.Factory}. */
  12. public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener) {
  13. DefaultDataSourceFactory upstreamFactory =
  14. new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener));
  15. return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache());
  16. }
  17. /** Returns a {@link HttpDataSource.Factory}. */
  18. public HttpDataSource.Factory buildHttpDataSourceFactory(
  19. TransferListener<? super DataSource> listener) {
  20. return new DefaultHttpDataSourceFactory(userAgent, listener);
  21. }
  22. private synchronized Cache getDownloadCache() {
  23. if (downloadCache == null) {
  24. File downloadContentDirectory = new File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY);
  25. downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor());
  26. }
  27. return downloadCache;
  28. }
  29. private static CacheDataSourceFactory buildReadOnlyCacheDataSource(
  30. DefaultDataSourceFactory upstreamFactory, Cache cache) {
  31. return new CacheDataSourceFactory(
  32. cache,
  33. upstreamFactory,
  34. new FileDataSourceFactory(),
  35. /* cacheWriteDataSinkFactory= */ null,
  36. CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,
  37. /* eventListener= */ null);
  38. }
  39. private File getDownloadDirectory() {
  40. if (downloadDirectory == null) {
  41. downloadDirectory = getExternalFilesDir(null);
  42. if (downloadDirectory == null) {
  43. downloadDirectory = getFilesDir();
  44. }
  45. }
  46. return downloadDirectory;
  47. }
  48. }

效果图:

   

  其他资料:

   exoPlayer地址

  exoplayer开发指南翻译

 demo下载

     








声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/299326
推荐阅读
相关标签
  

闽ICP备14008679号