I am trying to find a way to use the Spotify SDK in Android using Jetpack Compose in Kotlin. All the resources I've found till now about this seem to use Views and not Compose.
I still did the Spotify Get Started tutorial below: https://developer.spotify.com/documentation/android/tutorials/getting-started
I deviated from it in the following significant ways:
- I inherited my class from ComponentActivity() because AppCompatActivity() was crashing the app.
- I called a composable I made instead of setContentView()
- The part explaining how to import the spotify-app-remote-release-0.8.0.aar file seems to be for another version of AndroidStudio, so I imported it as below: https://developer.android.com/studio/projects/android-library#psd-add-aar-jar-dependency
I registered my app on Spotify for Developers, whitelisted the redirect URI and uploaded a SHA1 fingerprint.
I'm testing this app on my phone (Pixel 5 Android 14) with Spotify installed and logged into my premium account.
The app launches but no music comes out. Furthermore, the app doesn't seem to ever execute override fun onConnected() or override fun onFailure() according to logcat. Instead it's generating a lot of spotify related errors, some of which I'm posting below my code:
So here are my questions:
- Any idea why code does not work?
- Do you know of any other SpotifySDK for Android courses working with Jetpack Compose on Kotlin?
Any help is greatly appreciated!
MainActivity.kt:
package com.example.spotifyandroidsdk
/* Unused tutorial imports:
import android.support.v7.app.AppCompatActivity;
import com.spotify.protocol.client.Subscription;
import com.spotify.protocol.types.PlayerState;
*/
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import com.example.spotifyandroidsdk.ui.theme.SpotifyAndroidSDKTheme
import com.spotify.android.appremote.api.ConnectionParams
import com.spotify.android.appremote.api.Connector
import com.spotify.android.appremote.api.SpotifyAppRemote
import com.spotify.protocol.types.Track
//class MainActivity : AppCompatActivity() {
class MainActivity : ComponentActivity() {
private val clientId = "95583e167c84558e0fab519aede646b9"
private val redirectUri = "com.example.spotifyandroidsdk://callback"
private var spotifyAppRemote: SpotifyAppRemote? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
setContent { SpotifyAndroidSDKTheme { HomeScreen() } }
}
override fun onStart() {
super.onStart()
val connectionParams = ConnectionParams.Builder(clientId)
.setRedirectUri(redirectUri)
.showAuthView(true)
.build()
SpotifyAppRemote.connect(this, connectionParams, object : Connector.ConnectionListener {
override fun onConnected(appRemote: SpotifyAppRemote) {
spotifyAppRemote = appRemote
Log.d("MainActivity", "Connected! Yay!")
connected()
}
override fun onFailure(throwable: Throwable) {
Log.d("MainActivity", "onFailure()")
Log.e("MainActivity", throwable.message, throwable)
}
})
}
private fun connected() {
spotifyAppRemote?.let {
val playlistURI = "spotify:playlist:37i9dQZF1DX2sUQwD7tbmL"
it.playerApi.play(playlistURI)
it.playerApi.subscribeToPlayerState().setEventCallback {
val track: Track = it.track
Log.d("MainActivity", track.name + " by " + track.artist.name)
}
}
}
override fun onStop() {
super.onStop()
spotifyAppRemote?.let {
SpotifyAppRemote.disconnect(it)
}
}
}
// I added this myself...
@Composable
fun HomeScreen() {
Column (
verticalArrangement = Arrangement.SpaceEvenly,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Hello Spotify!")
}
}
Logcat errors:
2023-12-18 18:06:24.735 2552-2595 OpenGLRenderer com.android.systemui E Unable to match the desired swap behavior.
2023-12-18 18:06:25.131 28784-6507 Spotify com.spotify.music E [network@f:437] AP Socket Error: Software caused connection abort (103)
2023-12-18 18:06:25.131 28784-6507 Spotify com.spotify.music E [network@f:758] Connection error: ap_socket_reset
2023-12-18 18:06:25.141 28784-6512 Spotify com.spotify.music E [logging@f:221] aq: OnError reason: 1, error:Connection closed by peer
2023-12-18 18:06:25.205 28784-6505 Spotify com.spotify.music E [Core Thread@connect:459] ConnectConnectivityListener: received empty connection_id
2023-12-18 18:06:25.388 12022-12041 OpenGLRenderer com.example.spotifyandroidsdk E Unable to match the desired swap behavior.
2023-12-18 18:06:25.943 1731-4241 ActivityTaskManager system_server E Abort background activity starts from 10242
2023-12-18 18:06:25.984 28784-6505 Spotify com.spotify.music E [Core Thread@connect:463] ConnectConnectivityListener: received new_connection_id Y2VmYTJiMjUtYWJkMC00OTYxLWE5YTQtZjQ3N2U5MjBlODhhK2RlYWxlcit0Y3A6Ly9nZXcxLWRlYWxlci1hLXJnODIuZ2V3MS5zcG90aWZ5Lm5ldDo1NzAwKzg2RDU0QjgyNEExNjY2QUY4NTg4OTI5REIxQzUxOTM0RTZDMDdFNzkxNThDN0I1ODlGQkM3MjQxNUI2MjU0RDc= - starting
2023-12-18 18:06:26.024 1731-1994 DiscreteRegistry system_server E Deleting file 1702313906375tl
2023-12-18 18:06:26.025 1731-1994 DiscreteRegistry system_server E Deleting file 1702314138660tl
2023-12-18 18:06:28.855 14166-14227 OneSearchS...stProvider com...le.android.apps.nexuslauncher E Failed to fetch suggestions from AppSearch, fallback to AGA
java.util.concurrent.CompletionException: java.util.NoSuchElementException: No value present
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:308)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:323)
at java.util.concurrent.CompletableFuture$UniAccept.tryFire(CompletableFuture.java:684)
at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:486)
at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2138)
at com.google.android.apps.nexuslauncher.allapps.H0.onResult(Unknown Source:59)
at android.app.appsearch.SearchSessionUtil$1.lambda$onResult$0(SearchSessionUtil.java:129)
at android.app.appsearch.SearchSessionUtil$1.$r8$lambda$_nlk9WiPjSfaAsqKYmp0aPvSRC0(SearchSessionUtil.java:1)
at android.app.appsearch.SearchSessionUtil$1$$ExternalSyntheticLambda0.run(R8$$SyntheticClass:13)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:144)
at com.google.android.apps.nexuslauncher.allapps.H0.onResult(Unknown Source:16)
at android.app.appsearch.SearchSessionUtil$1.lambda$onResult$0(SearchSessionUtil.java:129)
at android.app.appsearch.SearchSessionUtil$1.$r8$lambda$_nlk9WiPjSfaAsqKYmp0aPvSRC0(SearchSessionUtil.java:1)
at android.app.appsearch.SearchSessionUtil$1$$ExternalSyntheticLambda0.run(R8$$SyntheticClass:13)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.os.HandlerThread.run(HandlerThread.java:67)
2023-12-18 18:06:28.866 28784-12073 Spotify com.spotify.music E [pool-161-thread-1] Could not send the message to the remote client
2023-12-18 18:06:28.871 14166-14216 OpenGLRenderer com...le.android.apps.nexuslauncher E Unable to match the desired swap behavior.
2023-12-18 18:06:29.213 661-661 BpTransact...edListener surfaceflinger E Failed to transact (-32)