How to control the default music player of android or any other player? By controlling i mean pause, play, next etc. Do i have to bind the service? I have tried to use the IMediaPlaybackService
but it is not working. There is certainly a way out as i have seen apps in the android market which controls the music player. Any idea?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Seems like the referenced KeyEvent methods don't work for me on the latest SDK. Using the AudioManager's dispatchMediaKeyEvent() method with a defined KeyEvent worked for me.
See: here
Code example:
you can by getting the audiomanager then sending commands to it.
these are the commands.
Problem with sending
Intent
for broadcast receiver is that, user has to open Music Player at least once, also, It does not work for many third party music players.The code given below works on all music players.
I have tested this code on Sony, Htc and Samsung devices.
This code imitates play/pause button action from hands-free(ear phones).
If you want to control the music player (the current playing one) there is a way to send KeyEvents to it:
This is a safer way, cause usually broadcasting keyEvent could cause to play more than one player at a time.
Note: it needs to have minSdk 19 or more.
There's a new method in Android 4.4 called RemoteController, but it's a pain in the arse and I couldn't get it to work. It also requires you to direct the user to the global security settings and allow you app access to all their notifications!!! I'm not sure what Google was thinking here.
Anyway I followed all the instructions butregisterRemoteController()
still returnsfalse
so there's little else I can do (apparently Google have never heard of error codes).Given that user2572182's method doesn't seem to work on Android 4.4 it seems this is impossible.Edit: I found an example app using
RemoteController
on XDA of all places (who knew there was useful content there?). It works on my phone, but you still need to tell your users to go and change some obscure security setting so it's not ideal:http://forum.xda-developers.com/showthread.php?p=51535568
Edit 2: Google have changed this again in Android "L". I guess they realised
RemoteControlClient
was too complicated.KeyEvent's don't work properly in newest Android, as well as don't support applications like Spotify. The best solution is to use MediaController. For each application that is currently playing and you want to control it you have to create different MediaController. It requires Context and MediaSession.Token. Token 'identifies' process that plays music. For example if you played Google Play Music and you now play Spotify you can create MediaControllers for both. How to retrieve tokens? There are two ways:
You check current state of session from MediaController:
and control app using transport controls, e.g.:
Depending on your minSDK use Compat or not version. You can cast MediaSession.Token to MediaSessionCompat.Token with: