0

when I use exoplayer I get a player is accessed on the wrong thread error. How can I solve this?

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

The player is started as a service via my BackgroundAudioService.class.

exoPlayer = new ExoPlayer.Builder(getApplicationContext()
            .build();

In the main thread my looper is running, the which updates the UI via exoplayer.getCurrentPosition().

public final Runnable updatePosition = new Runnable() {
    @Override
    public void run() {
        position  = BackgroundAudioService.getCurrentPostion();
    }
}

myHandler.postDelayed(updatePosition, myHandlerSleep);

I don't know how to solve this problem (which occurs just sometimes), please help.

Thanks Alejandro

2 Answers 2

1

I solved this by calling the status via a handler in the player's event listener. Starting a runnable from the listener which runs only when 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);
        }            
    }
};

The UI calls the current position the same way in a runnable.

I can't say whether this is the best way. but it's going well.

GGK

3
  • this doesn't work for me. Playing some minutes will work, then exoplayer crashes with same error Sep 8, 2022 at 6:00
  • 1
    then check, if You create a exoplayer instance in multiple places in your code Sep 8, 2022 at 6:51
  • found the problem, You were right and Your answer is correct..thanks Sep 12, 2022 at 6:55
0

This issue is from the Exoplayer Library if you are using version com.google.android.exoplayer:exoplayer:2.18.6 and has been fixed. Furthermore, You can check below threads https://github.com/google/ExoPlayer/commit/aa4e0080147fe76a6c0cca40b7eccdf45eb58028 https://github.com/google/ExoPlayer/issues/11149

1
  • thanks, but with 2.18.7 the behaviour still occurred for me, so the workaround from @GGK is still necessary Nov 7 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.