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

发布于 2024-10-19 18:30:29 字数 249 浏览 7 评论 0原文

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

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

我想:

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

我想从我的应用程序暂停和恢复默认音乐播放器的音乐。

可以这样做吗?

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:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

过气美图社 2024-10-26 18:30:29

以下代码将通过发送广播来暂停默认 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);
} 

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);
} 
灯下孤影 2024-10-26 18:30:29

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

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

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

  • 谷歌播放音乐。
  • Apple Music
  • OnePlus 音乐应用

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
皓月长歌 2024-10-26 18:30:29

伙计们,尝试遵循代码,它对我有用:

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

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);
你没皮卡萌 2024-10-26 18:30:29
// 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";
// 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";
べ映画 2024-10-26 18:30:29

它对我来说效果很好:

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

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);
唯憾梦倾城 2024-10-26 18:30:29

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

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

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

There is also another solution which could only pause music player which is playing :

  1. implements AudioManager.OnAudioFocusChangeListener
  2. override methods which is suggested by the 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);
        }
    }
    
无悔心 2024-10-26 18:30:29

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

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文