游戏玩m3u8文件Android

尝试使用videoview和mediaplayer播放m3u8文件的多种方式后,我决定放弃。 每次我玩m3u8文件,我只听到声音(请不要写堆栈溢出回答我的问题,我已经把他们都冲了出来)问问周围,终于知道exoplayer也许是我正在寻找。 然而,外行似乎是一个新手,我不能find任何适当的教程 。 这就是说我自己是一个新手,所有关于跟踪器和blabla的谈话似乎对我来说太复杂了。 我只希望能够在我的应用程序中打开所有我的m3u8文件, 而不必将它们传递给vlc或任何外部意图。

logging我使用KitKat和以上。 所以外行应该是可以实施的。

所以,我绝对要求的是一个简单的教程,如何使用exoplayer或其他方式播放我的m3u8文件 ,显示video和播放audio, 而不只是其中之一。 请不要把我链接到谷歌开发的exoplayer页面。 我也去过那里

提前致谢 :)

Solutions Collecting From Web of "游戏玩m3u8文件Android"

在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(); } 

这是使用ExoPlayer Libstream式传输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); } }); } 

我创build了一个演示应用程序来播放m3u8媒体文件

首先添加gradle依赖到你的文件

 compile 'com.google.android.exoplayer:exoplayer:r2.4.0' 

创build一个简单的布局文件,存储master.m3u8path作为input,并使用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> 

创build一个简单的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); } } 

目前没有关于ExoPlayer教程。 ExoPlayerMediaPlayer的最佳select,但目前不是新手友好的。

你要做的就是进入github页面,看看demo程序中的DemoPlayer类。

这个应用程序可以打开很多不同的格式,包括hls