当我发送到默认播放器
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
ACTIVITY.sendBroadcast(i);
它开始播放。当我发送
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "stop");
ACTIVITY.sendBroadcast(i);
它停止了。但是当我再次尝试再玩时,它就无法播放。它只播放一秒钟然后再次停止。我在日志中看到我得到了两个意图。该
intent.getBooleanExtra("playing", false);
第一次是“真实”,然后是“假”。当我尝试“togglepause”时,这种情况不会发生。我还注意到,当我发送播放时,Android音乐应用和Google Play音乐都会启动。
我发现Google Play音乐引起了这种情况。显然,两个应用程序都有意图,当我发送游戏时,音乐播放器会回答isPlaying = true
和Google Play音乐isPlaying = false
。如何修复或如何发送或仅接收默认播放器?
有点晚了,但发帖给其他正在寻找此事的人。您可以通过向系统发送已按下并释放媒体按钮的有序广播来控制默认媒体播放器。以下代码将根据当前状态播放或暂停音乐:
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);
您可以通过相同的方式提醒系统已按下下一个或上一个音轨按钮。
你没有发出“停止”命令,试过“暂停”吗?
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
似乎可以根据需要多次调用暂停/播放,并且重新启动就好了。