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);
}
相关文章
Android点击EditText文本框之外任何地方隐藏键盘的解决办法
这篇文章主要介绍了Android点击EditText文本框之外任何地方隐藏键盘的解决办法,非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-01-01Android中RecyclerView实现多级折叠列表效果(二)
这篇文章主要给大家介绍了Android中RecyclerView实现多级折叠列表的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。2017-05-05Android编程UI设计之GridView和ImageView的用法
这篇文章主要介绍了Android编程UI设计之GridView和ImageView的用法,结合实例形式较为详细的分析了Android中GridView和ImageView组件的相关方法使用技巧,需要的朋友可以参考下2016-01-01Android编程之SMS读取短信并保存到SQLite的方法
这篇文章主要介绍了Android编程之SMS读取短信并保存到SQLite的方法,涉及Android针对SMS短信及SQLite数据库的相关操作技巧,需要的朋友可以参考下2015-11-11
最新评论