尝试使用 videoview和mediaplayer 播放m3u8文件的多种方法后,我决定放弃。每次我播放m3u8文件时我都会听到声音。(请不要写堆栈溢出的网址来回答我的问题。我已经全部红了) 一直在四处询问,终于知道 exoplayer 可能是我正在寻找的那个。然而exoplayer似乎是一个新手,我可以找到任何适当的教程。有人说我自己是一个新手,关于跟踪器和blabla的所有谈话对我来说似乎太复杂了。我只希望能够在我的应用中打开来自不同网址的所有m3u8文件,而无需将它们传递给vlc或任何外部意图。
对于记录我使用KitKat及以上。所以exoplayer应该是可实现的。
所以我绝望地要求的是一个简单教程,介绍如何使用exoplayer播放我的m3u8文件或任何其他显示视频和播放音频的方式 NOT 只是其中之一。请不要将我链接到 google dev 上的exoplayer页面。我也去过那里。
提前致谢:)
答案 0 :(得分:4)
在Android 4.1及更高版本中,您可以使用此库https://github.com/brianwernick/ExoMedia/。 Read-me页面上提到的示例应该足以让您入门。我已经通过一些添加/修改重现了该代码片段。
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();
}
答案 1 :(得分:3)
我创建了一个演示应用来播放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活动来播放文件。
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);
}
}
答案 2 :(得分:2)
我使用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();
}
});
答案 3 :(得分:1)
这是使用ExoPlayer流式传输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);
}
});
}
答案 4 :(得分:0)
目前没有关于ExoPlayer
的教程。 ExoPlayer
是MediaPlayer
的最佳替代品,但目前不是新手友好的。
您需要做的是转到github页面并查看DemoPlayer
应用中的demo
课程。
此应用可以打开许多不同的格式,包括hls
。
答案 5 :(得分:0)
解决播放m3u8文件黑屏的问题,我用这个解决了,
<块引用>实现'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)