A way to control the default Android media player from my own service (pause/play/..)

maiconcwb

Member
Licensed User
Longtime User
Hi friends,

I'm looking for a way to control the default Android media player from my own service (or activity). :sign0085: I'm NOT interested in playing media from my service or activity, I'd like to do pause/play/stop/next/previous operations (maybe using Intents??) and, if possible, read track name and other information from the current song being played.

Thanks in advance!

P.S. Sorry my english...
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Widgetoid lets you make custom widgets, and it lets you control the default music player
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
When I click play:


When I click next track:

When I click previous:
Starting MediaPlaybackServiceImpl.

I also found this:
http://stackoverflow.com/questions/6352136/android-using-intents-to-control-media-player

And emailed the developer of widgetsoid. I'll tell you if I figure anything out
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
long eventtime = SystemClock.uptimeMillis();

  Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
  KeyEvent downEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
  downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
  sendOrderedBroadcast(downIntent, null);

  Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
  KeyEvent upEvent = new KeyEvent(eventtime, eventtime,
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
  upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
  sendOrderedBroadcast(upIntent, null);

We just need to convert that to B4A

But our intent object doesnt support Intent.ACTION_MEDIA_BUTTON
So you need to make a new topic asking Erel to add it.
 
Last edited:
Upvote 0

maiconcwb

Member
Licensed User
Longtime User
Thanks for fast replay!!

I'm new in android world, I dont't understand very well about intent, but I could do something:

Dim i As Intent
Dim p As Phone
i.Initialize("com.android.music.musicservicecommand","")
i.PutExtra("command", "togglepause")
Try
p.SendBroadcastIntent(i)
Catch
ToastMessageShow("Sorry, something wrong...", True)
End Try

...and it works! :icon_clap: (with player already open)

You can use stop/pause/previous/next.

Now, i have 50%, just need get current song name... possible?

thanks again!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…