视频播放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_8
targetCompatibility JavaVersion.VERSION_1_8
}
3 编写界面
<com.google.android.exoplayer2.ui.PlayerView
android: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 设置播放(最简单)
-
- 设置player参数,使用SimpleExoPlayer
-
- 设置MediaSource播放资源
-
- 为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.newSi