gpt4 book ai didi

java - ExoPlayer - java.lang.IllegalStateException : sending message to a Handler on a dead thread

转载 作者:行者123 更新时间:2023-12-02 10:11:29 73 4
gpt4 key购买 nike

我在 RecyclerView 中使用 SimpleExoPlayer,有时调用时会出现错误

simplePlayer.setPlayWhenReady(true);

视频播放器保持空白,并且出现问题的时刻并不总是相同(我没有找到模拟此问题的确切方法)

异常:

java.lang.IllegalStateException: Handler (android.os.Handler) {edbfbdd} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:545)
at android.os.Handler.enqueueMessage(Handler.java:662)
at android.os.Handler.sendMessageAtTime(Handler.java:631)
at android.os.Handler.sendMessageDelayed(Handler.java:601)
at android.os.Handler.sendMessage(Handler.java:538)
at android.os.Message.sendToTarget(Message.java:418)
at com.google.android.exoplayer2.ExoPlayerImplInternal.setPlayWhenReady(ExoPlayerImplInternal.java:183)
at com.google.android.exoplayer2.ExoPlayerImpl.setPlayWhenReady(ExoPlayerImpl.java:245)
at com.google.android.exoplayer2.SimpleExoPlayer.updatePlayWhenReady(SimpleExoPlayer.java:1188)
at com.google.android.exoplayer2.SimpleExoPlayer.setPlayWhenReady(SimpleExoPlayer.java:896)

我的代码在自定义 View 内运行(在回收器 View 适配器中更新):

MyView h = this;
h.playerView = new PlayerView(getContext());

h.playerView.setVisibility(View.GONE);
h.postFrameLayout.addView(h.playerView, 0);

h.postFrameLayout.getLayoutParams().width = swidth;
h.postFrameLayout.getLayoutParams().height = (int) (swidth );

h.playerView.getLayoutParams().width = swidth;
h.playerView.getLayoutParams().height = (int) (swidth );


if (player != null){
player.release();
}

DefaultLoadControl loadControl = new DefaultLoadControl.Builder()
.setBufferDurationsMs(1100, 2000, 1000, 1000).createDefaultLoadControl();
final SimpleExoPlayer simplePlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
h.playerView.setUseController(false);

h.playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH);
h.playerView.setPlayer(simplePlayer);

simplePlayer.addListener(new Player.EventListener() {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == Player.STATE_ENDED) {
h.videoPlay.setVisibility(View.VISIBLE);
h.postImage.setVisibility(View.VISIBLE);
simplePlayer.setPlayWhenReady(false);
simplePlayer.seekTo(0);
}
}
});
Uri uri = Uri.parse(videoUrl);
final MediaSource audioSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
simplePlayer.prepare(audioSource);

h.player = simplePlayer;

h.videoPlay.setVisibility(View.VISIBLE);
h.videoPlay.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
displayArticles(false, post.getPhotoItems(), h);

h.videoPlay.setVisibility(View.GONE);
h.postImage.setVisibility(View.GONE);
h.playerView.setVisibility(View.VISIBLE);
simplePlayer.setPlayWhenReady(true);

}
});

我在从窗口分离时释放播放器(当回收器 View 在我的自定义 View 上调用 onDetachedFromWindow() 时):

if (player != null) {
videoPlay.setVisibility(View.VISIBLE);
postImage.setVisibility(View.VISIBLE);
player.release();
player = null;
}

有没有办法避免此问题或在播放器启动时重试?

最佳答案

当我尝试使用 ..exoplayer2.ui.PlayerView 类时,我遇到了同样的问题。 ..exoplayer2.ui.PlayerControlView没有问题。这是我的代码:

 public static void setUp(Context context){
ExoPlayerTool.context = context;
player = new SimpleExoPlayer.Builder(context).build();
dataSourceFactory = new DefaultDataSourceFactory(context,
Util.getUserAgent(context, "SingerApp"));
}

public static void setSource(String link){
Uri uri = Uri.parse(link);
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
player.addListener(playerListener);
player.prepare(videoSource);
player.setPlayWhenReady(true);
}

我调用一次 setup() 函数,并在每次想要更改源时调用 setSource() 函数。

当我想关闭音乐时,我调用player.release();,并在释放setSource()后,在player.prepare上给出相同的错误(videoSource); 行。

我能够通过在每次源更改时创建新的 SimpleExoPlayer 来解决该问题。代码如下所示:

 public static void setSource(String link){
Uri uri = Uri.parse(link);
MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
player.release();
player = new SimpleExoPlayer.Builder(context).build();
player.addListener(playerListener);
player.prepare(videoSource);
player.setPlayWhenReady(true);
}

我不知道这个解决方案的优化程度如何,但总比根本不释放播放器要好。

关于java - ExoPlayer - java.lang.IllegalStateException : sending message to a Handler on a dead thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592280/

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