The irony is I've done this before, I even submitted a library for it. But none of my old code is working anymore.
Did Google screw up/with the API like they do everything else?
Did Google screw up/with the API like they do everything else?
B4X:
'Direction: 0=Play/Pause, 1=Next, -1=Prev
Sub MusicControl(Direction As Int)
Dim Data As Intent, P As Phone, Command As String, TheButton As Int = KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE, Method As Long = 3
If Direction = -1 Then TheButton = KeyCodes.KEYCODE_MEDIA_PREVIOUS
If Direction = 1 Then TheButton = KeyCodes.KEYCODE_MEDIA_NEXT
Select Case TheButton
Case KeyCodes.KEYCODE_MEDIA_NEXT: Command = "next"
Case KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE: Command = "togglepause"
Case KeyCodes.KEYCODE_MEDIA_PREVIOUS: Command = "previous"
Case KeyCodes.KEYCODE_MEDIA_STOP: Command = "stop"
Case Else: Return
End Select
Select Case Method
Case 0
Data.Initialize("android.intent.action.MEDIA_BUTTON", "")
If AndroidMediaPlayer.Length>0 Then Data.SetComponent(AndroidMediaPlayer)
Data.PutExtra("android.intent.extra.KEY_EVENT",MakeKeyEvent(0, TheButton))'needs to be passed as a keyevent, not an Int. 1 is up
P.SendBroadcastIntent(Data)
Data.Initialize("android.intent.action.MEDIA_BUTTON", "")
If AndroidMediaPlayer.Length>0 Then Data.SetComponent(AndroidMediaPlayer)
Data.PutExtra("android.intent.extra.KEY_EVENT",MakeKeyEvent(1, TheButton))'needs to be passed as a keyevent, not an Int. 1 is up
P.SendBroadcastIntent(Data)
Case 1
Dim MC As MediaController
If TheButton = KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE Then TheButton = KeyCodes.KEYCODE_HEADSETHOOK
MC.MediaButton(TheButton)
Case 2, 3
'If Not(IsPackagerunning(AndroidMediaPlayer, False)) Then StartPackage(AndroidMediaPlayer)
Dim I As Intent, P As Phone
If Method = 2 Then
I.Initialize("com.android.music.musicservicecommand", "")
Else
I.Initialize("com.android.music.musicservicecommand." & Command, "")
End If
If AndroidMediaPlayer.Length>0 Then I.SetComponent(AndroidMediaPlayer)
I.PutExtra("command", Command)
P.SendBroadcastIntent(I)
End Select
Log("media command (method " & Method & "): " & Command)
End Sub
'Action: 0=down, 1=up
Sub MakeKeyEvent(Action As Int, KeyCode As Int) As Object
Dim ke As JavaObject
ke.InitializeNewInstance("android.view.KeyEvent", Array As Object(Action, KeyCode))
Return ke
End Sub
Last edited: