如下暂停音乐播放代码:
private void pauseMusic() { Intent freshIntent = new Intent(); freshIntent.setAction("com.android.music.musicservicecommand.pause"); freshIntent.putExtra("command", "pause"); sendBroadcast(freshIntent); }音乐播放器接收广播的代码位于MediaPlaybackService.java文件中MediaPlaybackService类的成员变量mIntentReceiver的onReceive函数中。
MediaPlaybackService.java文件位于packagesappsmusicsrccomandroidmusic目录下
代码如下:
(1)参数定义:
- public static final String CMDPAUSE = "pause";
- public static final String PAUSE_ACTION = "com.android.music.musicservicecommand.pause";
- } else if (CMDPAUSE.equals(cmd) || PAUSE_ACTION.equals(action)) { // 接收到音乐暂停的广播
- if(isPlaying()){
- mPausedByOthers = true;
- }
- pause();
- mPausedByTransientLossOfFocus =false;
- }