我创建了一个演示应用程序来播放 m3u8 媒体文件
首先将 gradle 依赖项添加到您的文件中
compile 'com.google.android.exoplayer:exoplayer:r2.4.0'
创建一个简单的布局文件,其中存储 master.m3u8 路径作为输入,SimpleExoPlayerView 视图播放下载的文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout mlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.design.widget.TextInputEditText
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:id="@+id/mediaPath"
android:layout_height="56dp" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/play"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_below="@+id/mediaPath"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:text="Play"
android:layout_height="56dp" />
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:layout_below="@+id/play"
android:id="@+id/video_view"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
创建一个简单的 ExoPlayer Activity 来播放文件。
public class ExoPlayer extends AppCompatActivity{
Context mContext;
SimpleExoPlayerView playerView;
EditText editText;
private ComponentListener componentListener;
private DataSource.Factory mediaDataSourceFactory;
private Handler mainHandler;
private DefaultTrackSelector trackSelector;
SimpleExoPlayer player;
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exoplayer);
mContext =this;
editText = (EditText) findViewById(R.id.mediaPath);
Button btnPlay = (Button) findViewById(R.id.play);
playerView = (SimpleExoPlayerView)findViewById(R.id.video_view);
mediaDataSourceFactory = buildDataSourceFactory(true);
mainHandler = new Handler();
componentListener = new ComponentListener();
trackSelector = new DefaultTrackSelector();
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(editText.getText()!=null && editText.getText().length()>0){
initializePlayer(editText.getText().toString());
}
}
});
}
private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
return ((AnalyticsApplication) getApplication())
.buildDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);
}
private void initializePlayer(String path) {
player = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
player.addListener(componentListener);
// String path = file:///storage/emulated/0/SugarBox/master.m3u8";
Uri uri = Uri.parse(path);
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
playerView.setPlayer(player);
}
private MediaSource buildMediaSource(Uri uri) {
return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, null);
}
private void releasePlayer() {
if (player != null) {
player.removeListener(componentListener);
player.release();
player = null;
}
}
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
if(editText.getText()!=null && editText.getText().length()>0){
initializePlayer(editText.getText().toString());
}
}
}
@Override
public void onResume() {
super.onResume();
if ((Util.SDK_INT <= 23 || player == null)) {
if(editText.getText()!=null && editText.getText().length()>0){
initializePlayer(editText.getText().toString());
}
}
}
@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT <= 23) {
releasePlayer();
}
}
@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT > 23) {
releasePlayer();
}
}
private class ComponentListener implements com.google.android.exoplayer2.ExoPlayer.EventListener{
@Override
public void onPlayerStateChanged(boolean playWhenReady,
int playbackState) {
String stateString;
switch (playbackState) {
case com.google.android.exoplayer2.ExoPlayer.STATE_IDLE:
stateString = "ExoPlayer.STATE_IDLE -";
break;
case com.google.android.exoplayer2.ExoPlayer.STATE_BUFFERING:
stateString = "ExoPlayer.STATE_BUFFERING -";
break;
case com.google.android.exoplayer2.ExoPlayer.STATE_READY:
stateString = "ExoPlayer.STATE_READY -";
break;
case com.google.android.exoplayer2.ExoPlayer.STATE_ENDED:
stateString = "ExoPlayer.STATE_ENDED -";
break;
default:
stateString = "UNKNOWN_STATE -";
break;
}
Log.d("ExopLayer", "changed state to " + stateString
+ " playWhenReady: " + playWhenReady);
}
@Override
public void onTimelineChanged(Timeline timeline, Object manifest) {}
@Override
public void onTracksChanged(TrackGroupArray trackGroups,
TrackSelectionArray trackSelections) {}
@Override
public void onLoadingChanged(boolean isLoading) {}
@Override
public void onPlayerError(ExoPlaybackException error) {}
@Override
public void onPositionDiscontinuity() {}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {}
}
private DrmSessionManager<FrameworkMediaCrypto> buildDrmSessionManager(UUID uuid, String licenseUrl, String[] keyRequestPropertiesArray) throws UnsupportedDrmException {
if (Util.SDK_INT < 18) {
return null;
}
HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(licenseUrl,
buildHttpDataSourceFactory(false));
if (keyRequestPropertiesArray != null) {
for (int i = 0; i < keyRequestPropertiesArray.length - 1; i += 2) {
drmCallback.setKeyRequestProperty(keyRequestPropertiesArray[i],
keyRequestPropertiesArray[i + 1]);
}
}
return new DefaultDrmSessionManager<>(uuid,
FrameworkMediaDrm.newInstance(uuid), drmCallback, null, mainHandler, null);
}
private HttpDataSource.Factory buildHttpDataSourceFactory(boolean useBandwidthMeter) {
return ((AnalyticsApplication) getApplication())
.buildHttpDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);
}
}