本文目录
1 ExoPlayer介绍
1.1 概述
ExoPlayer是Google出品的视频播放器,功能强大,使用简单,独立于Android代码框架,以一个开源代码库的形式存在,可定制性强。
ExoPlayer也是Google官方推荐的Android媒体播放器,可以在Android官方文档的音频和视频目录中找到。
ExoPlayer和MediaPlayer相比,多了支持基于 HTTP 的动态自适应流 (DASH)、SmoothStreaming 和通用加密等功能。
git仓库地址:GitHub - google/ExoPlayer: An extensible media player for Android
1.2 优点
- 支持DASH和SmoothStreaming这两种数据格式的资源,而MediaPlayer对这两种数据格式都不支持。它还支持其它格式的数据资源,比如MP4, M4A, FMP4, WebM, MKV, MP3, Ogg, WAV, MPEG-TS, MPEG-PS, FLV and ADTS (AAC)等
- 支持高级的HLS特性,比如能正确的处理#EXT-X-DISCONTINUITY标签
- 无缝连接,合并和循环播放多媒体的能力
- 和应用一起更新播放器(ExoPlayer),因为ExoPlayer是一个集成到应用APK里面的库,你可以决定你所想使用的ExoPlayer版本,并且可以随着应用的更新把ExoPlayer更新到一个最新的版本。
- 较少的关于设备的特殊问题,并且在不同的Android版本和设备上很少会有不同的表现。
- 在Android4.4(API level 19)以及更高的版本上支持Widevine通用加密
- 为了符合你的开发需求,播放器支持自定义和扩展。其实ExoPlayer为此专门做了设计,并且允许很多组件可以被自定义的实现类替换。
- 使用官方的扩展功能可以很快的集成一些第三方的库,比如IMA扩展功能通过使用互动媒体广告SDK可以很容易地将视频内容货币化(变现)
1.3 缺点
在某些设备上播放音频,ExoPlayer可能会比MediaPlayer消耗更多的电量。
2 准备工作
2.1 添加仓库
在项目 build.gradle 中添加 google(),如下:
repositories {
google()
jcenter()
}
2.2 添加依赖
在模块 build.gradle 中的 dependencies 下添加 ExoPlayer 框架依赖。
2.2.1 整体引用
implementation 'com.google.android.exoplayer:exoplayer:2.X.X'
通过上述方法,引入整个ExoPlayer库。
2.2.2 按需引用
也可以根据自己的需求选择性添加依赖,如核心库和UI库,这两个可以满足基本上的视频播放需求。
implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'
整个ExoPlayer库包含5个字库,依赖5个子库和依赖整个库效果是一样的。
- exoplayer-core:核心功能 (必要)
- exoplayer-dash:支持DASH内容
- exoplayer-hls:支持HLS内容
- exoplayer-smoothstreaming:支持SmoothStreaming内容
- exoplayer-ui:用于ExoPlayer的UI组件和相关的资源。
2.3 添加Java8的支持
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
2.4 权限
如需播放本地文件,需要在 AndroidManifest.xml 中的 manifest 标签(application 同级标签)下添加外部存储读写权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
如需播放网络流,需要增加网络权限。
<uses-permission android:name="android.permission.INTERNET" />
3 播放HLS流
在实际项目中,需要在安卓4.4(好老的版本啊...)的板卡上实现HLS流播放。
测试了ExoPlayer几个版本,发现2.11.1可以运行在安卓4.4上。
3.1 依赖库
implementation 'com.google.android.exoplayer:exoplayer:2.11.1'
3.2 MainActivity.java
public class MainActivity extends Activity{
private static String TAG = "MainActivity";
private SimpleExoPlayer player;
//apple的HLS流测试地址
private String testUrl = "http://devimages.apple.com/iphone/samples/bipbop/gear3/prog_index.m3u8";
public void initPlayer(){
PlayerView playerView = findViewById(R.id.player_view);
SimpleExoPlayer player = new SimpleExoPlayer.Builder(this).build();
playerView.setPlayer(player);
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, ""));
MediaSource videoSource = new HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(testUrl));
// Prepare the player with the source.
player.prepare(videoSource);
player.setPlayWhenReady(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initPlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (player != null) {
player.release();
player=null;
}
}
}
3.3 布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
>
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>