问题
In my Android application, I am playing different mp3 files using the MediaPlayer class.
The user is playing his/her own music with the default music application.
I want to:
- Pause the music of the default music application.
- Start playing music from my application.
- When my music is done playing, my app will resume the user's default music.
I want to pause and resume the music of the default music player from my application.
Is it possible to do this?
回答1:
The following code will pause a default MediaPlayer by sending a broadcast:
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isMusicActive()) {
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
YourApplicationClass.this.sendBroadcast(i);
}
回答2:
// pause
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
sendBroadcast(i);
// play
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
sendBroadcast(i);
// next
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "next");
sendBroadcast(i);
// previous
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "previous");
sendBroadcast(i);
Some more info about available commands:
public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMDSTOP = "stop";
public static final String CMDPAUSE = "pause";
public static final String CMDPLAY = "play";
public static final String CMDPREVIOUS = "previous";
public static final String CMDNEXT = "next";
回答3:
To control currently playing music use this code.
AudioManager mAudioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
if(mode == Config.MUSIC_NEXT) {
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
mAudioManager.dispatchMediaKeyEvent(event);
}else if(mode == Config.MUSIC_PLAY){
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
mAudioManager.dispatchMediaKeyEvent(event);
}
else if(mode == Config.MUSIC_PREV){
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
mAudioManager.dispatchMediaKeyEvent(event);
}
infact this is the only code that is working for all music apps i used. checked with
- Google play music.
- Apple Music
- OnePlus music app
回答4:
guys,try follow code,it worked for me:
// stop music player
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(null,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
//resume music player
am.abandonAudioFocus(null);
回答5:
It is working very well for me :
KeyEvent ke = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MEDIA_PAUSE);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
// construct a PendingIntent for the media button and unregister it
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
PendingIntent pi = PendingIntent.getBroadcast(context,
0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
intent.putExtra(Intent.EXTRA_KEY_EVENT, ke);
sendKeyEvent(pi,context, intent);
ke = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE);
intent.putExtra(Intent.EXTRA_KEY_EVENT, ke);
sendKeyEvent(pi, context, intent);
回答6:
There is also another solution which could only pause music player which is playing :
- implements AudioManager.OnAudioFocusChangeListener
- override methods which is suggested by the ide.
call this function when it is needed :
private void checkAudioFocus() { AudioManager mAudioManager = (AudioManager) getApplicationContext().getSystemService(AUDIO_SERVICE); if(mAudioManager.isMusicActive()) { int result = mAudioManager.requestAudioFocus(AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } }
来源:https://stackoverflow.com/questions/5129027/android-application-to-pause-resume-the-music-of-another-music-player-app