devgianlu / librespot-android

A demo app that runs librespot-java on Android
33 stars 13 forks source link

librespot-android crashes if i stop playback from spotify app by changing device #15

Closed vhaudiquet closed 2 years ago

vhaudiquet commented 2 years ago

If i start playing a song on librespot-android, and then go to my spotify app to change the source of the song, librespot-android crashes :

2022-01-06 23:23:07.091 20210-20258/xyz.gianlu.librespot.android E/AndroidRuntime: FATAL EXCEPTION: player-audio-sink
    Process: xyz.gianlu.librespot.android, PID: 20210
    java.lang.IllegalStateException: Unable to retrieve AudioTrack pointer for stop()
        at android.media.AudioTrack.native_stop(Native Method)
        at android.media.AudioTrack.stop(AudioTrack.java:2881)
        at xyz.gianlu.librespot.android.sink.AndroidSinkOutput.stop(AndroidSinkOutput.java:99)
        at xyz.gianlu.librespot.player.mixing.AudioSink.run(AudioSink.java:160)
        at java.lang.Thread.run(Thread.java:920)

This is caused by the line : if (track != null) track.stop(); (AndroidSinkOutput.java:99)

I tried to fix the issue, and i have a bizarre behavior ; this code :

if (track != null)
{
    System.out.println("STATE:" + track.getPlayState());
    track.stop();
}

does not crash the player. Maybe i was lucky, or maybe getPlayState() actualizes something inside the AudioTrack ?

Anyway, the state here prints '1' in this case, i.e. AudioTrack.PLAYSTATE_STOPPED ; so a correct fix should be :

if (track != null && track.getPlayState() != AudioTrack.PLAYSTATE_STOPPED)
    track.stop();

I'm not sure if it is the "good way" though...

mitschwimmer commented 2 years ago

Thanks for reporting @vhaudiquet. I will look into this as soon as I find the time.