gpt4 book ai didi

android - 怎么用exoplayer玩m3u8,黑屏

转载 作者:行者123 更新时间:2023-12-04 23:43:50 25 4
gpt4 key购买 nike

尝试使用 exoplayer v2.10.5 播放 m3u8 时变黑,这让我添加了或者我如何使用 exoplayer v2.10.5 和 gradle 3.5.3 播放 m3u
或者哪个版本的 exoplayer 可以与 api 28 和 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);
}

关于android - 怎么用exoplayer玩m3u8,黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61220057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com