1. 引入ExoPlayer
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId ""
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//引入地方一
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
//引入地方二
compile 'com.google.android.exoplayer:exoplayer:r2.5.2'
}
2. ExoPlayer自定义View的使用
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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="com.example.lq.exoplayerdemo.PlayListActivity">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
如上,ExoPlayer提供了一个SimpleExoPlayerView给我们使用。
3. 实现视频的播放
- a. 创建
DefaultTrackSelector
对象,即磁道选择工厂对象
//创建带宽对象
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
//根据当前宽带来创建选择磁道工厂对象
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
//传入工厂对象,以便创建选择磁道对象
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
- b. 创建播放器对象,并与View进行绑定
//根据选择磁道创建播放器对象
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
//将player和view绑定
simpleExoPlayerView.setPlayer(player);
player.setPlayWhenReady(shouldAutoPlay);
- c. 创建数据源,并添加到播放器中
//定义数据源工厂对象
DataSource.Factory mediaDataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "ExoPlayerDemo"));
//创建Extractor工厂对象,用于提取多媒体文件
DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
//创建数据源
MediaSource mediaSource = new ExtractorMediaSource(
Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"),
mediaDataSourceFactory, extractorsFactory, null, null);
//添加数据源到播放器中
player.prepare(mediaSource);
完整代码如下:
public class VideoActivity extends AppCompatActivity {
private SimpleExoPlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
private DefaultTrackSelector trackSelector;
private boolean shouldAutoPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
}
private void initPlayer() {
simpleExoPlayerView = findViewById(R.id.player_view);
simpleExoPlayerView.requestFocus();
shouldAutoPlay = true;
//创建带宽对象
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
//根据当前宽带来创建选择磁道工厂对象
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
//传入工厂对象,以便创建选择磁道对象
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
//根据选择磁道创建播放器对象
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
//将player和view绑定
simpleExoPlayerView.setPlayer(player);
player.setPlayWhenReady(shouldAutoPlay);
//定义数据源工厂对象
DataSource.Factory mediaDataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "ExoPlayerDemo"));
//创建Extractor工厂对象,用于提取多媒体文件
DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
//创建数据源
MediaSource mediaSource = new ExtractorMediaSource(
Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"),
mediaDataSourceFactory, extractorsFactory, null, null);
//添加数据源到播放器中
player.prepare(mediaSource);
}
private void releasePlayer() {
if (player != null) {
shouldAutoPlay = player.getPlayWhenReady();
//释放播放器对象
player.release();
player = null;
trackSelector = null;
}
}
@Override
protected void onStart() {
super.onStart();
initPlayer();
}
@Override
protected void onResume() {
super.onResume();
if (player == null) {
initPlayer();
}
}
@Override
protected void onPause() {
super.onPause();
releasePlayer();
}
@Override
protected void onStop() {
super.onStop();
releasePlayer();
}
}