ExoPlayer-java.lang.IllegalStateException:在死线程上向处理程序发送消息

时间:2019-04-09 11:55:12

标签: java android exoplayer

我在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)

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

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);

            }
        }); 

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

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

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

1 个答案:

答案 0 :(得分:1)

当我尝试使用 .. 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);
}

我不知道此解决方案的优化程度如何,但总比不发布播放器好。