简体   繁体   English

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

[英]Android application to Pause/Resume the music of another music player app

In my Android application, I am playing different mp3 files using the MediaPlayer class.在我的 Android 应用程序中,我使用 MediaPlayer 类播放不同的 mp3 文件。

The user is playing his/her own music with the default music application.用户正在使用默认音乐应用程序播放他/她自己的音乐。

I want to:我想要:

  1. Pause the music of the default music application.暂停默认音乐应用程序的音乐。
  2. Start playing music from my application.从我的应用程序开始播放音乐。
  3. 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?是否有可能做到这一点?

The following code will pause a default MediaPlayer by sending a broadcast:以下代码将通过发送广播来暂停默认的 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);
} 

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

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一加音乐应用
// 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";

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

There is also another solution which could only pause music player which is playing :还有另一种解决方案只能暂停正在播放的音乐播放器:

  1. implements AudioManager.OnAudioFocusChangeListener实现 AudioManager.OnAudioFocusChangeListener
  2. override methods which is suggested by the ide.覆盖ide建议的方法。
  3. 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); } }

you try this code, hope help you.你试试这个代码,希望对你有帮助。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM