android暂停或停止其他音乐播放器的播放实现代码
代码如下:
public static final string playstate_changed = "com.android.music.playstatechanged";
public static final string meta_changed = "com.android.music.metachanged";
public static final string queue_changed = "com.android.music.queuechanged";
public static final string playback_complete = "com.android.music.playbackcomplete";
public static final string async_open_complete = "com.android.music.asyncopencomplete";
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 cmdprevious = "previous";
public static final string cmdnext = "next";
private broadcastreceiver mintentreceiver = new broadcastreceiver() {
@override
public void onreceive(context context, intent intent) {
string action = intent.getaction();
string cmd = intent.getstringextra("command");
if (cmdnext.equals(cmd) || next_action.equals(action)) {
next(true);
} else if (cmdprevious.equals(cmd) || previous_action.equals(action)) {
prev();
} else if (cmdtogglepause.equals(cmd) || togglepause_action.equals(action)) {
if (isplaying()) {
pause();
} else {
play();
}
} else if (cmdpause.equals(cmd) || pause_action.equals(action)) {
pause();
} else if (cmdstop.equals(cmd)) {
pause();
seek(0);
} else if (mediaappwidgetprovider.cmdappwidgetupdate.equals(cmd)) {
// someone asked us to refresh a set of specific widgets, probably
// because they were just added.
int[] appwidgetids = intent.getintarrayextra(appwidgetmanager.extra_appwidget_ids);
mappwidgetprovider.performupdate(mediaplaybackservice.this, appwidgetids);
}
}
};
通过分析可以得出要暂停音乐可以有两种方法,一种是直接发送cmd,另外一种是发送action。
如下暂停音乐播放代码:
private void pausemusic() {
intent freshintent = new intent();
freshintent.setaction("com.android.music.musicservicecommand.pause");
freshintent.putextra("command", "pause");
sendbroadcast(freshintent);
}