Control the default music player of android or any other music player

后端 未结 7 1079
抹茶落季
抹茶落季 2020-11-28 06:05

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 <

7条回答
  •  长情又很酷
    2020-11-28 06:55

    If you want to control the music player (the current playing one) there is a way to send KeyEvents to it:

    if (mAudioManager == null) mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
    
    KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
    mAudioManager.dispatchMediaKeyEvent(event);
    

    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.

提交回复
热议问题