經過廣播Intent控制Android系統自帶的音樂播放器java
ZhengZhirenandroid
http://blog.csdn.net/ZhengZhiRen/archive/2011/01/13/6134730.aspxgit
Android自帶的音樂播放器只能算是系統之上的應用,並不包含在SDK中,所以SDK文檔中沒有說起。可是,有時咱們須要控制系統音樂播放器的行爲,好比切換歌曲、暫停/繼續等。這就只能從這個應用的源代碼着手了。app
這是播放器的代碼樹:ide
http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=tree函數
能夠看到,音樂的播放是由MediaPlaybackService這個服務實現的:oop
http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob;f=src/com/android/music/MediaPlaybackService.java;h=7c0bbedbedef49bf4c38ef97fdcd0f1017bf5d35;hb=HEADthis
在服務的onCreate函數中,註冊了IntentFilter和BroadcastReceiver。spa
- public void onCreate() {
- super.onCreate();
- ……
- IntentFilter commandFilter = new IntentFilter();
- commandFilter.addAction(SERVICECMD);
- commandFilter.addAction(TOGGLEPAUSE_ACTION);
- commandFilter.addAction(PAUSE_ACTION);
- commandFilter.addAction(NEXT_ACTION);
- commandFilter.addAction(PREVIOUS_ACTION);
- registerReceiver(mIntentReceiver, commandFilter);
- ……
- }
這些Action常量在類的開頭定義爲:.net
- 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";
- public static final String TOGGLEPAUSE_ACTION = "com.android.music.musicservicecommand.togglepause";
- public static final String PAUSE_ACTION = "com.android.music.musicservicecommand.pause";
- public static final String PREVIOUS_ACTION = "com.android.music.musicservicecommand.previous";
- public static final String NEXT_ACTION = "com.android.music.musicservicecommand.next";
BroadcastReceiver接收和IntentFilter匹配的Intent。根據intent.getAction或intent.getStringExtra("command")的內容,進行相應操做,控制播放器的播放。
- private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- String cmd = intent.getStringExtra("command");
- MusicUtils.debugLog("mIntentReceiver.onReceive " + action + " / " + cmd);
- 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();
- mPausedByTransientLossOfFocus = false;
- } else {
- play();
- }
- } else if (CMDPAUSE.equals(cmd) || PAUSE_ACTION.equals(action)) {
- pause();
- mPausedByTransientLossOfFocus = false;
- } else if (CMDSTOP.equals(cmd)) {
- pause();
- mPausedByTransientLossOfFocus = false;
- 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);
- }
- }
- };
由此,咱們能夠經過廣播intent控制播放器了:
- Intent intent = new Intent("上面那幾個常量");
- sendBroadcast(intent);
參考資料:
http://www.poemcode.net/2009/11/android_music/
http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=tree