[英]how to play m3u8 with exoplayer, the screen remains black
goes black trying to play m3u8 with exoplayer v2.10.5,which makes me add or how can I play m3u with exoplayer v2.10.5 and gradle 3.5.3 or which version of exoplayer would work with api 28 and gradle 3.5.3
如果你能幫助我適應代碼或解釋它,更好的是我從這個開始,這將是一個幫助,mp4 播放沒有問題。
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import androidx.appcompat.app.AppCompatActivity;
/**
* A fullscreen activity to play audio or video streams.
*/
public class PlayerActivity extends AppCompatActivity {
private PlayerView playerView;
private SimpleExoPlayer player;
private boolean playWhenReady=true;
private long playbackPosition=0;
private int currentWindow=0;
public String urlStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
urlStream = this.getIntent().getStringExtra("URL");
playerView = findViewById(R.id.video_view);
}
@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT < 24) {
releasePlayer();
}
}
@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT >= 24) {
releasePlayer();
}
}
private void releasePlayer() {
if (player != null) {
playWhenReady = player.getPlayWhenReady();
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentWindowIndex();
player.release();
player = null;
}
}
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT >= 24) {
initializePlayer();
}
}
@Override
public void onResume() {
super.onResume();
hideSystemUi();
if ((Util.SDK_INT < 24 || player == null)) {
initializePlayer();
}
}
@SuppressLint("InlinedApi")
private void hideSystemUi() {
playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
private void initializePlayer() {
player = ExoPlayerFactory.newSimpleInstance(this);
playerView.setPlayer(player);
Uri uri = Uri.parse(urlStream);
MediaSource mediaSource = buildMediaSource(uri);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
player.prepare(mediaSource, false, false);
}
private MediaSource buildMediaSource(Uri uri) {
DataSource.Factory dataSourceFactory =
new DefaultDataSourceFactory(this, "exoplayer-codelab");
return new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
}
} ```
您需要使用HLS媒體源來播放 m3u8 文件。
用這個
private MediaSource buildMediaSource(Uri uri) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, "exoplayer-codelab");
return new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
}
而不是這個
private MediaSource buildMediaSource(Uri uri) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, "exoplayer-codelab");
return new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.