且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的默认媒体播放器和QUOT;启动"后"停止"错误(com.android.music.musicservicecommand)

更新时间:2022-06-04 07:46:47

一个有点晚,但发布为别人谁正在寻找这一点。您可以通过发送一个有序的广播到媒体按钮已被pressed及以下released.The code将发挥或者根据当前状态,暂停音乐系统控制默认媒体播放器:

A little late but posting for others who are searching for this. You can control default media player by sending an ordered broadcast to the system that a media button has been pressed and released.The following code will play or pause music depending on the current state:

  long eventtime = SystemClock.uptimeMillis(); 
  Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 
  KeyEvent downEvent = new KeyEvent(eventtime, eventtime, 
  KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); 
  downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); 
  sendOrderedBroadcast(downIntent, null); 

  Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 
  KeyEvent upEvent = new KeyEvent(eventtime, eventtime, 
  KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); 
  upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); 
  sendOrderedBroadcast(upIntent, null); 

您可以提醒,未来或previous轨道按钮已被pressed系统以同样的方式。

The same way you can alert the system that the next or previous track button has been pressed.