首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在错误的线程异常上访问Exoplayer抛出播放器

在错误的线程异常上访问Exoplayer抛出播放器
EN

Stack Overflow用户
提问于 2022-09-06 20:41:43
回答 1查看 209关注 0票数 0

当我使用exoplayer时,我会得到一个player is accessed on the wrong thread error。我怎么才能解决这个问题?

代码语言:javascript
运行
复制
Non-fatal Exception: java.lang.IllegalStateException: Player is accessed on the wrong 
thread.
Current thread: 'main'
Expected thread: 'ConnectivityThread'
See https://exoplayer.dev/issues/player-accessed-on-wrong-thread

播放机是通过我的BackgroundAudioService.class作为服务启动的。

代码语言:javascript
运行
复制
exoPlayer = new ExoPlayer.Builder(getApplicationContext()
            .build();

在主线程中,我的活套正在运行,它通过exoplayer.getCurrentPosition()更新UI。

代码语言:javascript
运行
复制
public final Runnable updatePosition = new Runnable() {
    @Override
    public void run() {
        position  = BackgroundAudioService.getCurrentPostion();
    }
}

myHandler.postDelayed(updatePosition, myHandlerSleep);

我不知道如何解决这个问题(有时会发生),请帮助。

谢谢亚历杭德罗

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-06 22:11:14

我通过在播放器事件侦听器中的一个处理程序调用状态来解决这个问题。从侦听器启动runnable,该监听器仅在player.isPlaying() == true运行时运行。

代码语言:javascript
运行
复制
     player.addListener(new Player.Listener() {
        @Override
        public void onEvents(Player player, Player.Events events) {
            Player.Listener.super.onEvents(player, events);

            if (events.contains(Player.EVENT_IS_PLAYING_CHANGED)) {
                if (player.isPlaying()) {
                    positionHandler.postDelayed(getCurrentPositionTask,CURRENT_POSITION_SLEEP);

                } else {
                    positionHandler.removeCallbacks(getCurrentPositionTask);

                }
            }
        }
    });

    public Runnable getCurrentPositionTask = new Runnable() {

    @Override
    public void run() {
        if (exoPlayer != null) {
            currentPostion = exoPlayer.getCurrentPosition();
            positionHandler.postDelayed(getCurrentPositionTask,CURRENT_POSITION_SLEEP);
        }            
    }
};

UI在runnable中以相同的方式调用当前位置。

我不能说这是不是最好的方法。但进展得很好。

GGK

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73627564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档