Android Tutorial How to get track change data

ie: If you want to make a music control widget

First you need to add a broadcast listener to the manifest

AddReceiverText(thenameofyourservice,
<intent-filter>
<action android:name="com.android.music.metachanged" />'stock music player
<action android:name="com.miui.player.metachanged" />'MIUI music player
<action android:name="com.htc.music.metachanged" />'HTC music player
<action android:name="com.nullsoft.winamp.metachanged" />'WinAmp
<action android:name="com.real.IMP.metachanged" />'MyTouch4G
</intent-filter>)

Then add this code to your Service_Start sub

B4X:
If StartingIntent.HasExtra("track") Then
        MusicCommand =  GetExtra(StartingIntent, "command", "")
        MusicTrack = GetExtra(StartingIntent, "track", "UNKNOWN TRACK")
        MusicAlbum = GetExtra(StartingIntent, "album", "UNKNOWN ALBUM")
        MusicArtist = GetExtra(StartingIntent, "artist", "UNKNOWN ARTIST")
        Log("Track changed to: " & MusicTrack & " (" & MusicAlbum & " - " & MusicArtist & ")")'substitute with whatever code you want to handle the change
    End If

This to your Process_Globals

B4X:
Dim MusicArtist As String = "NO ARTIST", MusicAlbum As String = "NO ALBUM", MusicTrack As String = "NO TRACK", MusicCommand As String

And this anywhere in the service

B4X:
Sub GetExtra(StartingIntent As Intent, Extra As String, Default As String) As String
    If startingintent.HasExtra(extra) Then Return startingintent.GetExtra(extra) Else Return default
End Sub
 
Top