spotify / android-sdk

Spotify SDK for Android
https://developer.spotify.com/documentation/android/
Apache License 2.0
462 stars 119 forks source link

SpotifyAppRemote.connect gets stuck indeterminately, 0.8.0, 0.7.2, multiple Android devices and OS #348

Open Fot1s opened 1 year ago

Fot1s commented 1 year ago

*Hi, we are trying to use Spotify App Remote library to control spotify via voice control inside our application. Our issue is that from time to time, SpotifyAppRemote.connect fails to connect or report an exception. It seems that Spotify's service gets in a 'stuck' state where it cannot respond back to client apps. When this happens, trying to drop / reconnect does not resolve the issue, but a new connection seem to happen, which is then immediately problematic (same stuck state). After some indeterminate time that varies from some seconds to several minutes, the situation seems to fix it self, and at that point a number of disconnection and timeout exceptions are reported one after the other for the previously made connection attempts.

Bringing Spotify in the foreground, restarting the app, or killing the background service does not seem to help.

This makes the sdk unusable for us, could you investigate? Some code attached bellow:

*

Issue found on 07.JUL.23.

SDK Version: 0.8.0, 0.7.2

OS Version: 11, 12, 13

Code Sample:

We initially (once / on app install) use the following code to get app-remote-control and authorise our app

    val builder = AuthorizationRequest.Builder(CLIENT_ID, AuthorizationResponse.Type.CODE, REDIRECT_URI)

    builder.setScopes(
        arrayOf(
            "streaming",
            "app-remote-control",
            "user-read-email",
            "user-read-private",
            "user-read-playback-state",
            "user-modify-playback-state",
            "user-top-read",
            "user-follow-modify",
            "user-library-modify",
            "user-read-currently-playing",
            "user-read-recently-played"
        )
    )
    val request = builder.build()

    AuthorizationClient.openLoginActivity(this, REQUEST_CODE, request)

As soon as we have a result in onActivityResult, following code is used to access the remote control (and on further app uses):

    val connectionParams = ConnectionParams.Builder(CLIENT_ID)
        .setRedirectUri(REDIRECT_URI)
        .showAuthView(false)
        .build()

    SpotifyAppRemote.connect(
        this, connectionParams,
        object : Connector.ConnectionListener {

            override fun onConnected(appRemote: SpotifyAppRemote) {
               ... //get and use the remote
            }

            override fun onFailure(throwable: Throwable) {

                when (throwable) {
                    is CouldNotFindSpotifyApp -> {
                        Toast.makeText(applicationContext, "SPT Please install Spotify first!", Toast.LENGTH_LONG).show()
                    }
                    is UserNotAuthorizedException -> {
                        authorizeAndGetSpotifyCodeViaActivityResult()
                    }
                    is SpotifyConnectionTerminatedException -> {
                        Timber.e("SPT Connection was dropped! Trying reconnecting here!")
                    }
                    else ->
                        Timber.e(throwable, "SPT Spotify onFailure!")
                }
            }
        }
    )

Steps to reproduce:

  1. it sometimes works others not, we have not found a way to constantly break it or use it

Expected behaviour:

App connects or an exception is given

Actual behaviour:

App gets stuck waiting for a connection or exception

richardrevesz commented 10 months ago

I think it is related to this:

https://github.com/spotify/android-sdk/issues/322?fbclid=IwAR0XXvon1Y_XqqQeplLOafBfvNB-M_W6o-FMjomtK08xdPbTIV0zUUPCWVU