Android应用程序暂停/恢复另一个音乐播放器应用程序的音乐

时间:2011-02-26 18:52:15

标签: android android-music-player

在我的Android应用程序中,我使用MediaPlayer类播放不同的mp3文件。

用户正在使用默认音乐应用程序播放他/她自己的音乐。

我想:

  1. 暂停默认音乐应用程序的音乐。
  2. 从我的应用程序开始播放音乐。
  3. 当我的音乐播放完毕后,我的应用程序将恢复用户的默认音乐。
  4. 我想从我的应用程序暂停并恢复默认音乐播放器的音乐。

    是否可以这样做?

7 个答案:

答案 0 :(得分:8)

以下代码将通过发送广播来暂停默认的MediaPlayer:

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);
} 

答案 1 :(得分:3)

// 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);

有关可用命令的更多信息:

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";

答案 2 :(得分:2)

要控制当前播放的音乐,请使用此代码。

    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);
            }

事实上,这是我用过的所有音乐应用程序的唯一代码。 检查

  • Google播放音乐。
  • Apple Music
  • OnePlus音乐应用

答案 3 :(得分:1)

伙计们,请尝试按照代码,它对我有用:

// 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);

答案 4 :(得分:1)

对我来说效果很好:

    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);

答案 5 :(得分:1)

还有另一种解决方案,只能暂停正在播放的音乐播放器:

  1. 实现AudioManager.OnAudioFocusChangeListener
  2. ide建议的替代方法。
  3. 在需要时调用此函数:

    private void checkAudioFocus() {
        AudioManager mAudioManager = (AudioManager) getApplicationContext().getSystemService(AUDIO_SERVICE);  
        if(mAudioManager.isMusicActive())
        {
            int result = mAudioManager.requestAudioFocus(AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }
    }
    

答案 6 :(得分:0)

你试试这个代码,希望对你有帮助。

val audioManager = requireActivity().getSystemService(Context.AUDIO_SERVICE) as AudioManager   val focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run {
        setAudioAttributes(AudioAttributes.Builder().run {
            setUsage(AudioAttributes.USAGE_GAME)
            setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            setOnAudioFocusChangeListener({  }, object : Handler() {})
            build()
        })
        setAcceptsDelayedFocusGain(true)
        build()
    }
    audioManager.requestAudioFocus(focusRequest)