Android Code Snippet Display Spotify Song Info

ac9ts

Active Member
Licensed User
I wrote an app for myself to use when running. I always listen to Spotify and I would hear a song that I didn't know. Rather than playing around with the phone to see what it was, I added some code to display it. This is the stripped down source. Maybe somebody could use it.

It requires the BroadcastReceiver library https://www.b4x.com/android/forum/threads/broadcastreceiver.12493/

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: SpotifyReceiver
    #VersionCode: 1
    #VersionName: 
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals

End Sub

Sub Globals

    Dim Broadcast As BroadCastReceiver
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    Broadcast.Initialize("BroadcastReceiver")
   
    Broadcast.addAction("com.spotify.music.playbackstatechanged")
    Broadcast.addAction("com.spotify.music.metadatachanged")
    Broadcast.addAction("com.spotify.music.queuechanged")
    Broadcast.registerReceiver("")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

    If UserClosed Then Broadcast.unregisterReceiver
   
End Sub

Sub BroadcastReceiver_OnReceive (Action As String, Intent As Object)

    Dim I As Intent = Intent
   
    Log(Action)
   
    Select Action
        Case "com.spotify.music.playbackstatechanged"
            Log(I.getExtra("playing"))
            Log(I.getExtra("playbackPosition"))
        Case "com.spotify.music.metadatachanged"
            Log(I.getExtra("id"))
            Log(I.getExtra("artist"))
            Log(I.getExtra("album"))
            Log(I.getExtra("track"))
            Log(I.getExtra("length"))
    End Select
   
    Log("----------------------")

End Sub
More info at https://developer.spotify.com/documentation/android/guides/android-media-notifications/
 
Top