当前位置: 代码迷 >> Android >> [AndroidTips]透过广播Intent控制Android系统自带的音乐播放器
  详细解决方案

[AndroidTips]透过广播Intent控制Android系统自带的音乐播放器

热度:314   发布时间:2016-05-01 20:12:17.0
[AndroidTips]通过广播Intent控制Android系统自带的音乐播放器

http://blog.csdn.net/ZhengZhiRen/article/details/6134730

?

Android自带的音乐播放器只能算是系统之上的应用,并不包含在SDK中,因此SDK文档中没有提及。但是,有时我们需要控制系统音乐播放器的行为,比如切换歌曲、暂停/继续等。这就只能从这个应用的源代码着手了。

?

这是播放器的代码树:

http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=tree

?

可以看到,音乐的播放是由MediaPlaybackService这个服务实现的:

http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob;f=src/com/android/music/MediaPlaybackService.java;h=7c0bbedbedef49bf4c38ef97fdcd0f1017bf5d35;hb=HEAD

?

在服务的onCreate函数中,注册了IntentFilter和BroadcastReceiver。

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常量在类的开头定义为:

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

?

  相关解决方案