android-sdk icon indicating copy to clipboard operation
android-sdk copied to clipboard

Unbale to connect spotify remote from android

Open MirzaAhmedBaig opened this issue 4 years ago • 6 comments

I am trying to get spotify remote as follworing

SpotifyAppRemote.connect(application,
                ConnectionParams.Builder(getString(R.string.SPOTIFY_CLIENT_ID))
                    .setRedirectUri(getString(R.string.SPOTIFY_REDIRECT_URI))
                    .showAuthView(true)
                    .build(),
                object : Connector.ConnectionListener {
                    override fun onConnected(spotifyAppRemote: SpotifyAppRemote) {
                        mSpotifyAppRemote = spotifyAppRemote
                        Log.d(TAG, "Connected! Yay!")
                        // Now you can start interacting with App Remote
                        connected()
                    }

                    override fun onFailure(throwable: Throwable) {
                        if (throwable is NotLoggedInException || throwable is UserNotAuthorizedException) {
                            Log.e(TAG, "Not logged in : ${throwable.message}")
                        } else if (throwable is CouldNotFindSpotifyApp) {
                            Log.e(TAG, "Not downloaded : ${throwable.message}")
                        }

                        Log.e(TAG, throwable.message, throwable)
                    }
                })

but there is no call back after this. A notification is coming "App is connecting" but there is nothing after that no call back no error nothing

MirzaAhmedBaig avatar Oct 07 '20 08:10 MirzaAhmedBaig

Im having the same problem

Diegofr104 avatar Oct 08 '20 01:10 Diegofr104

I had the same issue. The documentation is not very clear IMO. From what I understood, you need first to authorize your application.

For this, you'll need first to add the authentification library into your project:

implementation 'com.spotify.android:auth:1.2.3

Then, into your onCreate() you'll be able to authorize your application using:

val request: AuthorizationRequest =
            AuthorizationRequest.Builder(getString(R.string.SPOTIFY_CLIENT_ID), AuthorizationResponse.Type.TOKEN, getString(R.string.SPOTIFY_REDIRECT_URI))
                .setScopes(arrayOf("app-remote-control"))
                .build()

AuthorizationClient.openLoginActivity(this, 1337, request)

The code above will open Spotify's LoginActivity which will ask the user to authorize your app (if needed, depending the scopes you've requested in the builder).

You'll then get a response into onActivityResult() :

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1337) {
            val response: AuthorizationResponse = AuthorizationClient.getResponse(resultCode, data)

            when (response.type) {
                AuthorizationResponse.Type.TOKEN -> {
                   // do whatever you need with the access token `response.accessToken`

                   // ADD YOUR CODE HERE and it should be able to play !
                }

                AuthorizationResponse.Type.ERROR -> {
                    Log.e(TAG, "Auth error : " + response.error)
                }

                else -> {
                    Log.e(TAG, "Auth result: " + response.type)
                }
            }
        }
    }

pghazal avatar Oct 09 '20 13:10 pghazal

Yeah, the onActivityResult is what'll help you out. I can confirm this

joseandroidengineer avatar Oct 09 '20 19:10 joseandroidengineer