Android Question How do you control the media player from another app? / Or use a context object within inline Java?

NeoTechni

Well-Known Member
Licensed User
Longtime User
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?

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:

NeoTechni

Well-Known Member
Licensed User
Longtime User
Google says the recent Java code is:
B4X:
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
audio.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Got it working as a Class module
B4X:
Sub Class_Globals
	Private nativeMe As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
	nativeMe = Me
End Sub

public Sub SendKey(KeyCode As Int)
	Log(nativeMe.RunMethod("audiocontrol", Array (KeyCode)))
End Sub

#If JAVA
	import android.content.Context;
	import android.media.AudioManager;
	import android.view.KeyEvent;
	import anywheresoftware.b4a.keywords.Common;
	public void audiocontrol(int KeyCode){	
		Context context = BA.applicationContext;
		AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		audio.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyCode));
		audio.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyCode));
	}
#End If

Then use this from anywhere else

B4X:
public Sub ControlAudio(KeyCode As Int)
	Dim AC As AudioControl
	AC.Initialize
	AC.SendKey(KeyCode)
End Sub
 
Upvote 0
Top