当我使用exoplayer
时,我会得到一个player is accessed on the wrong thread error
。我怎么才能解决这个问题?
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作为服务启动的。
exoPlayer = new ExoPlayer.Builder(getApplicationContext()
.build();
在主线程中,我的活套正在运行,它通过exoplayer.getCurrentPosition()
更新UI。
public final Runnable updatePosition = new Runnable() {
@Override
public void run() {
position = BackgroundAudioService.getCurrentPostion();
}
}
myHandler.postDelayed(updatePosition, myHandlerSleep);
我不知道如何解决这个问题(有时会发生),请帮助。
谢谢亚历杭德罗
发布于 2022-09-06 22:11:14
我通过在播放器事件侦听器中的一个处理程序调用状态来解决这个问题。从侦听器启动runnable
,该监听器仅在player.isPlaying() == true
运行时运行。
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
https://stackoverflow.com/questions/73627564
复制相似问题