视频播放ExoPlayer

GitHub

https://github.com/google/ExoPlayer

1. 介绍

ExoPlayer是一款适用于Android的应用程序级媒体播放器。它为Android的MediaPlayer API提供了一个替代方案,可以在本地和互联网上播放音频和视频。ExoPlayer支持Android的MediaPlayer API目前不支持的功能,包括DASH和SmoothStreaming自适应回放。与MediaPlayer API不同,ExoPlayer易于定制和扩展,并且可以通过Play Store应用程序更新进行更新。

2. 简单使用

相比于原生的videoview,非常重要的一点就是播放播放的资源不是直接通过videoview.setUri()方法直接实现。exoplayer有一个专门管理播放资源的东西MediaSource。

1 导入依赖

最新的依赖版本请见GitHub,演示采用2.11.7版本

//exoplayer
implementation 'com.google.android.exoplayer:exoplayer:2.11.7'
2.加入JAVA 1.8支持
compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8
}
3 编写界面
<com.google.android.exoplayer2.ui.PlayerViewandroid:id="@+id/exo_playerview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="40dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toTopOf="@id/playercontrol"/>
4 设置播放(最简单)
    1. 设置player参数,使用SimpleExoPlayer
    1. 设置MediaSource播放资源
    1. 为playerview设置资源
// 1.设置player参数
private SimpleExoPlayer player;/** The scheme part of a raw resource URI. */
public static final String RAW_RESOURCE_SCHEME = "rawresource";private void initializePlayer() {if (player==null){player = ExoPlayerFactory.newSimpleInstance(this);exoPlayerView.setPlayer(player);                  //这个exoPlayer就是界面上的//设置播放(准备好立刻播放)player.setPlayWhenReady(playWhenReady);player.seekTo(currentWindow, playbackPosition);}// Produces DataSource instances through which media data is loaded.DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,Util.getUserAgent(this, String.valueOf(getApplication())));// This is the MediaSource representing the media to be played.ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();// 2.设置MediaSource播放资源MediaSource videoSource = new ExtractorMediaSource(Uri.parse(Uri.parse(RAW_RESOURCE_SCHEME + ":///" + R.raw.media_test).toString()),dataSourceFactory, extractorsFactory, null, null);// Prepare the player with the source.// 3.为playerview设置资源player.prepare(videoSource);
}

3. 总结

ExoPlayer定制化其实很高,可以像上面那样简单实现,也可以个性化定制,具体可见源码。

同时也有大佬为这个东西写了一个简单的封装工具类:

https://www.jianshu.com/p/547dc4a8ebe4

public class ExoPlayerManger {private static final String TAG = "ExoPlayerManger";private Context mContext;private BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();// 创建轨道选择工厂private TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);// 创建轨道选择器实例private TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);private SimpleExoPlayer simpleExoPlayer;private DataSource.Factory dataSourceFactory;private String mVideoUrl;private SimpleCache simpleCache;private Uri playVideoUri;private ExtractorMediaSource mediaSource;/*** @param context 传入context*/public void setBuilderContext(Context context) {mContext = context;dataSourceFactory = new DefaultDataSourceFactory(mContext, "seyed");}/*** @param videoUrl 传入视频路径*/public void setVideoUrl(String videoUrl) {this.mVideoUrl = videoUrl;simpleCache = VideoCache.getInstance(mContext);playVideoUri = Uri.parse(mVideoUrl);}/*** @return 返回exoPlayer对象*/public SimpleExoPlayer create() {try {simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);dataSourceFactory = new CacheDataSourceFactory(simpleCache, dataSourceFactory);mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(playVideoUri);simpleExoPlayer.prepare(mediaSource);} catch (Exception e) {}return simpleExoPlayer;}}
public class VideoCache {private static SimpleCache sDownloadCache;/*** @param context* @return*/public static SimpleCache getInstance(Context context) {if (sDownloadCache == null) {sDownloadCache = new SimpleCache(new File(getMediaCacheFile(context), "StoryCache"), new LeastRecentlyUsedCacheEvictor(512 * 1024 *1024));}return sDownloadCache;}public static File getMediaCacheFile(Context context) {String directoryPath = "";String childPath = "exoPlayer";if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 外部储存可用directoryPath = File.separator + context.getExternalFilesDir(childPath).getAbsolutePath();} else {directoryPath = File.separator + context.getFilesDir().getAbsolutePath() + File.separator + childPath;}File file = new File(directoryPath);//判断文件目录是否存在if (!file.exists()) {file.mkdirs();}return file;}}

使用如下:

     ExoPlayerManger exoPlayerManger = new ExoPlayerManger();exoPlayerManger.setBuilderContext(this);//设置Uri// exoPlayerManger.setVideoUrl(playVideoUrl);//设置从raw下读取的文件路径exoPlayerManger.setVideoUrl(RawResourceDataSource.buildRawResourceUri(R.raw.media_test).toString());SimpleExoPlayer simpleExoPlayer = exoPlayerManger.create();//设置音量simpleExoPlayer.setVolume(10);simpleExoPlayer.setVolume(0);// simpleExoPlayer.setRepeatMode(1);playerView.setPlayer(simpleExoPlayer);//监听(可自定义拓展)//simpleExoPlayer.addListener(this);//开启播放simpleExoPlayer.setPlayWhenReady(true);

4.GitHub小demo地址

https://github.com/ONLY-yours/ExoPlayerDemo

视频播放ExoPlayer(附小demo)相关推荐

  1. 视频播放ExoPlayer(附GitHub Demo)

    视频播放ExoPlayer GitHub https://github.com/google/ExoPlayer 1. 介绍 ExoPlayer是一款适用于Android的应用程序级媒体播放器.它为A ...

  2. 多功能视频播放组件演示Demo

    组件功能定义  1.支持普通视频文件播放.快进(16x)放慢(1/8x),速率可调   2.支持单帧向前向后.支持帧号.百分比定位:   3.支持大华录像下载文件以及丢失时间戳视频文件:   4.自适 ...

  3. 【翻译】安卓新播放器EXOplayer介绍

    [翻译]安卓新播放器EXOplayer介绍 http://developer.android.com/guide/topics/media/exoplayer.html 前言: Playing vid ...

  4. 推荐几款比较好Android视频播放器

    1.ijkplayer 项目地址: https://github.com/Bilibili/ijkplayer 介绍:Ijkplayer 是Bilibili发布的基于 FFplay 的轻量级 Andr ...

  5. 完整视频播放器封装库

    2019独角兽企业重金招聘Python工程师标准>>> 目录介绍 1.关于此视频封装库介绍 1.1 能够满足那些业务需求 1.2 对比同类型的库有哪些优势 2.关于使用方法说明 2. ...

  6. 完整视频播放器封装库,仿优酷

    目录介绍 1.关于此视频封装库介绍 1.1 能够满足那些业务需求 1.2 对比同类型的库有哪些优势 2.关于使用方法说明 2.1 关于gradle引用说明 2.2 添加布局 2.3 最简单的视频播放器 ...

  7. 完整视频播放器封装库 1

    目录介绍 1.关于此视频封装库介绍 1.1 能够满足那些业务需求 1.2 对比同类型的库有哪些优势 2.关于使用方法说明 2.1 关于gradle引用说明 2.2 添加布局 2.3 最简单的视频播放器 ...

  8. ExoPlayer 开发者指导

    ExoPlayer 开发者指导 字数2134 阅读12051 评论36 喜欢32 原文地址 想深入了解ExoPlayer的童鞋可以查看我的另外一篇文章:ExoPlayer源码浅析 Developer ...

  9. 简易网络视频播放器android

    简易网络视频播放器android demo: Qvod 1.新建: app\src\main\res\xml\network_security_config.xml 作用是可以发送 http请求 &l ...

最新文章

  1. CodeForces 721C Journey
  2. RHEL6.4更改为CentOS6.4的yum源
  3. Qt 设置textEdit插入文本的字体、大小和颜色
  4. Kaggle 数据清洗挑战 Day 5 - 处理不一致数据
  5. vue 调用webservice_调用webService的几种方式
  6. 女程序员做了个梦,众网友的神回复
  7. NYOJ277 - 车牌号
  8. fix ubuntu boot(grub)
  9. 2021外卖CPS分销微信小程序源码 外卖侠CPS全套源码
  10. sps的process插件安装包_SPSS的Process插件下载和安装
  11. hashcat破解密码规则示例
  12. pt100热电阻计算公式C语言,PT100计算公式
  13. 松翰单片机--SN8F5702学习笔记(七)TIMER0、TIMER1
  14. 微信V3接口商家转账到零钱
  15. java计算机毕业设计网上花店源码+系统+mysql数据库+LW文档+部署文件
  16. 形式化验证1——modex工具学习
  17. excel提取每一行或每一列的最后一个数据
  18. mysql服务器默认使用用户_在Windows下配置MySql服务器默认使用的用户是
  19. 整数反转----秦九昭算法
  20. 根据词云寻找对应文章的Web开发

热门文章

  1. java正则表达 w_正则表达式 \w \d 的意义
  2. (已解决)WPS如何插入公式和数学表达式 // WPS如何插入网上文章的表达式
  3. POJ 1094拓补排序
  4. Spring默认使用的JSON工具--Jackson
  5. 项目反应理论 matlab,多维项目反应理论等级反应模型.pdf
  6. SQL怎么实现模糊查询
  7. 351岁的同仁堂“病”了吗?
  8. 宁波三中机器人_宁波晚报
  9. 商务快车软件测试大乐,商务快车这个软件有用过的吗?效果怎么样? 爱问知识人...
  10. 基于MQTT的数据采集系统