Exoplayer 播放 m3u8 文件 Android
Exoplayer playing m3u8 files Android
在尝试了多种使用videoview和mediaplayer播放m3u8文件的方法后,我决定放弃。每次我播放 m3u8 文件时,我只听到声音。(请不要写来自堆栈溢出的 url 来回答我的问题。我已经把它们都红色了)
四处打听,终于知道 exoplayer 也许就是我要找的那个。但是 exoplayer 似乎是个新手,我找不到任何合适的 教程。有人说我自己是新手,所有关于 tracker 和 blabla 的讨论对我来说似乎都太复杂了。我只希望能够在我的应用程序 中打开来自不同 url 的所有 m3u8 文件,而无需 将它们传递给 vlc 或任何外部意图。
郑重声明,我使用 KitKat 及以上版本。所以exoplayer应该是可以实现的。
所以我迫切需要的是一个关于如何使用 exoplayer 或任何其他显示视频和播放音频的方式播放我的 m3u8 文件的简单教程 NOT只是其中之一。请不要 link 我到 google dev 上的 exoplayer 页面。我也去过
提前致谢:)
ExoPlayer
目前没有教程。 ExoPlayer
是 MediaPlayer
的最佳替代品,但目前对新手不是很友好。
你要做的就是去github页面看看demo
应用中的DemoPlayer
class。
此应用程序可以打开许多不同的格式,包括 hls
。
这是使用 ExoPlayer Lib 流式传输 m3u8 文件的最简单方法希望对您有所帮助
https://github.com/karim23/SimpleStreamPlayer/tree/master
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
setContentView(R.layout.activity_main);
//change the live streaming URL with yours.
contentUri = "http://abclive.abcnews.com/i/abc_live4@136330/index_1200_av-b.m3u8?sd=10&b=1200&rebase=on";
// contentType = DemoUtil.TYPE_HLS;
final Intent intent = new Intent(context, VideoPlayerActivity.class).setData(Uri.parse(contentUri))
.putExtra(VideoPlayerActivity.CONTENT_ID_EXTRA, -1)
//Change the type according to the live streaming extension.
.putExtra(VideoPlayerActivity.CONTENT_TYPE_EXTRA, DemoUtil.TYPE_HLS);
liveStreamingTv =(TextView)findViewById(R.id.mainActivity_liveStreamingTv);
liveStreamingTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
}
在 Android 4.1+ 上,您可以使用此库 https://github.com/brianwernick/ExoMedia/。 Read-me 页面中提到的示例应该足以帮助您入门。我用一些 additions/modifications.
复制了该代码片段
private void setupVideoView() {
EMVideoView emVideoView = (EMVideoView)findViewById(R.id.video_play_activity_video_view);
emVideoView.setOnPreparedListener(this);
//Enter your m3u8 URL below
emVideoView.setVideoURI(Uri.parse("http://SOMESERVER/playlist.m3u8"));
}
@Override
public void onPrepared(MediaPlayer mp) {
//Starts the video playback as soon as it is ready
emVideoView.start();
}
@Override
public void onPause() {
super.onPause();
//Pause Video Playback
emVideoView.pause();
}
我已经创建了一个演示应用程序来播放 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);
}
}
我已经使用以下代码使用 m3u8
videoView = findViewById(R.id.videoViewXml);
final ProgressDialog progressDialog = new ProgressDialog(VideoActivity.this);
progressDialog.setMessage("Please Wait");
progressDialog.setCancelable(false);
progressDialog.show();
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(VIDEO_PATH));
videoView.start();
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
}
});
解决播放m3u8文件黑屏的问题,我已经用这个解决了,
implementation 'com.google.android.exoplayer:exoplayer:2.13.3'
媒体源代码:
private fun buildMediaSource(uri: Uri): MediaSource {
val userAgent = "exoplayer-codelab"
return if (uri.lastPathSegment!!.contains("mp3") || uri.lastPathSegment!!.contains("mp4")) {
ExtractorMediaSource.Factory(DefaultHttpDataSourceFactory(userAgent))
.createMediaSource(uri)
} else {
HlsMediaSource.Factory(DefaultHttpDataSourceFactory(userAgent))
.createMediaSource(uri)
}
}
媒体来源:
val mediaSource = buildMediaSource(Uri.parse(stories[counter].mediaUrl))
simpleExoPlayer.prepare(mediaSource, false, false)
在尝试了多种使用videoview和mediaplayer播放m3u8文件的方法后,我决定放弃。每次我播放 m3u8 文件时,我只听到声音。(请不要写来自堆栈溢出的 url 来回答我的问题。我已经把它们都红色了) 四处打听,终于知道 exoplayer 也许就是我要找的那个。但是 exoplayer 似乎是个新手,我找不到任何合适的 教程。有人说我自己是新手,所有关于 tracker 和 blabla 的讨论对我来说似乎都太复杂了。我只希望能够在我的应用程序 中打开来自不同 url 的所有 m3u8 文件,而无需 将它们传递给 vlc 或任何外部意图。
郑重声明,我使用 KitKat 及以上版本。所以exoplayer应该是可以实现的。
所以我迫切需要的是一个关于如何使用 exoplayer 或任何其他显示视频和播放音频的方式播放我的 m3u8 文件的简单教程 NOT只是其中之一。请不要 link 我到 google dev 上的 exoplayer 页面。我也去过
提前致谢:)
ExoPlayer
目前没有教程。 ExoPlayer
是 MediaPlayer
的最佳替代品,但目前对新手不是很友好。
你要做的就是去github页面看看demo
应用中的DemoPlayer
class。
此应用程序可以打开许多不同的格式,包括 hls
。
这是使用 ExoPlayer Lib 流式传输 m3u8 文件的最简单方法希望对您有所帮助 https://github.com/karim23/SimpleStreamPlayer/tree/master
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
setContentView(R.layout.activity_main);
//change the live streaming URL with yours.
contentUri = "http://abclive.abcnews.com/i/abc_live4@136330/index_1200_av-b.m3u8?sd=10&b=1200&rebase=on";
// contentType = DemoUtil.TYPE_HLS;
final Intent intent = new Intent(context, VideoPlayerActivity.class).setData(Uri.parse(contentUri))
.putExtra(VideoPlayerActivity.CONTENT_ID_EXTRA, -1)
//Change the type according to the live streaming extension.
.putExtra(VideoPlayerActivity.CONTENT_TYPE_EXTRA, DemoUtil.TYPE_HLS);
liveStreamingTv =(TextView)findViewById(R.id.mainActivity_liveStreamingTv);
liveStreamingTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
}
在 Android 4.1+ 上,您可以使用此库 https://github.com/brianwernick/ExoMedia/。 Read-me 页面中提到的示例应该足以帮助您入门。我用一些 additions/modifications.
复制了该代码片段 private void setupVideoView() {
EMVideoView emVideoView = (EMVideoView)findViewById(R.id.video_play_activity_video_view);
emVideoView.setOnPreparedListener(this);
//Enter your m3u8 URL below
emVideoView.setVideoURI(Uri.parse("http://SOMESERVER/playlist.m3u8"));
}
@Override
public void onPrepared(MediaPlayer mp) {
//Starts the video playback as soon as it is ready
emVideoView.start();
}
@Override
public void onPause() {
super.onPause();
//Pause Video Playback
emVideoView.pause();
}
我已经创建了一个演示应用程序来播放 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);
}
}
我已经使用以下代码使用 m3u8
videoView = findViewById(R.id.videoViewXml);
final ProgressDialog progressDialog = new ProgressDialog(VideoActivity.this);
progressDialog.setMessage("Please Wait");
progressDialog.setCancelable(false);
progressDialog.show();
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(VIDEO_PATH));
videoView.start();
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
}
});
解决播放m3u8文件黑屏的问题,我已经用这个解决了,
implementation 'com.google.android.exoplayer:exoplayer:2.13.3'
媒体源代码:
private fun buildMediaSource(uri: Uri): MediaSource {
val userAgent = "exoplayer-codelab"
return if (uri.lastPathSegment!!.contains("mp3") || uri.lastPathSegment!!.contains("mp4")) {
ExtractorMediaSource.Factory(DefaultHttpDataSourceFactory(userAgent))
.createMediaSource(uri)
} else {
HlsMediaSource.Factory(DefaultHttpDataSourceFactory(userAgent))
.createMediaSource(uri)
}
}
媒体来源:
val mediaSource = buildMediaSource(Uri.parse(stories[counter].mediaUrl))
simpleExoPlayer.prepare(mediaSource, false, false)