Android Code Snippet Display Spotify Song Info

Discussion in 'Code Snippets' started by ac9ts, Dec 21, 2018.

  1. ac9ts

    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/

    Code:
    #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 StringIntent 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/
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice