MediaSession 简介

MediaSession 简介

本文主要是介绍下MediaSession,结合framework源码例子,最后看如何使用MediaSession 来监听A2DP的播放行为

MediaSession 主要是用来控制播放行为,如播放、暂停等行为,不过这个控制行为是由另外一个进程来操作的,举个例子,比如文件管理器进程在播放视频,此时你可以通过语音助手识别语音暂停、快进等命令,然后通过MediaSession将你的控制行为直接传输到文件管理器中来实现播放控制行为,就可以理解是跨进程通信的一组接口。

API介绍

Android reference doc MediaSession Android Developers

如何实现一个MediaSession 服务端

Using a media session

Implement a media session

这里有一篇结合ExoPlayer 使用MediaSession的文档

Controlling media through MediaSession

AVRCP协议和A2DP 的MediaSession 控制

AVRCP协议的全称是音视频远端控制协议,结合MediaSession框架能够很容易的实现音视频的播放控制,使用时我们只需要实现好客户端的代码,服务端由framework 中buletooth 实现。

接下来看Android Framework中的实例,来理解对MediaSession 相关使用和逻辑,这边是Android P源码,其相关的服务 A2dpMediaBrowserService.java

服务端实现

1、MediaBrowseService

首先可以看 A2dpMediaBrowserService 这个类的实现了MediaBrowserService 服务端

packages/apps/Bluetooth/src/com/android/bluetooth/a2dpsink/mbs/A2dpMediaBrowserService.java

摘下其注释

/**
 * Implements the MediaBrowserService interface to AVRCP and A2DP
 *
 * This service provides a means for external applications to access A2DP and AVRCP.
 * The applications are expected to use MediaBrowser (see API) and all the music
 * browsing/playback/metadata can be controlled via MediaBrowser and MediaController.
 *
 * The current behavior of MediaSession exposed by this service is as follows:
 * 1\. MediaSession is active (i.e. SystemUI and other overview UIs can see updates) when device is
 * connected and first starts playing. Before it starts playing we do not active the session.
 * 1.1 The session is active throughout the duration of connection.
 * 2\. The session is de-activated when the device disconnects. It will be connected again when (1)
 * happens.
 */
public class A2dpMediaBrowserService extends MediaBrowserService {
    // ...
}

可以先看下 MediaBrowserService 实现,其继承自 Service

/**
 * Base class for media browser services.
 * <p>
 * Media browser services enable applications to browse media content provided by an application
 * and ask the application to start playing it. They may also be used to control content that
 * is already playing by way of a {@link MediaSession}.
 * </p>
 *
 * To extend this class, you must declare the service in your manifest file with
 * an intent filter with the {@link #SERVICE_INTERFACE} action.
 *
 * For example:
 * </p><pre>
 * <service android:name=".MyMediaBrowserService"
 *          android:label="@string/service_name" >
 *     <intent-filter>
 *         <action android:name="android.media.browse.MediaBrowserService" />
 *     </intent-filter>
 * </service>
 * </pre>
 *
 */
public abstract class MediaBrowserService extends Service {
    // ....
}

MediaBrowserService 抽象了 onGetRoot 和 onLoadChildren 接口出来,所以子类要实现这两个接口。

    /**
     * Called to get the root information for browsing by a particular client.
     * <p>
     * The implementation should verify that the client package has permission
     * to access browse media information before returning the root id; it
     * should return null if the client is not allowed to access this
     * information.
     * </p>
     *
     * @param clientPackageName The package name of the application which is
     *            requesting access to browse media.
     * @param clientUid The uid of the application which is requesting access to
     *            browse media.
     * @param rootHints An optional bundle of service-specific arguments to send
     *            to the media browser service when connecting and retrieving the
     *            root id for browsing, or null if none. The contents of this
     *            bundle may affect the information returned when browsing.
     * @return The {@link BrowserRoot} for accessing this app's content or null.
     * @see BrowserRoot#EXTRA_RECENT
     * @see BrowserRoot#EXTRA_OFFLINE
     * @see BrowserRoot#EXTRA_SUGGESTED
     */
    public abstract @Nullable BrowserRoot onGetRoot(@NonNull String clientPackageName,
            int clientUid, @Nullable Bundle rootHints);

    /**
     * Called to get information about the children of a media item.
     * <p>
     * Implementations must call {@link Result#sendResult result.sendResult}
     * with the list of children. If loading the children will be an expensive
     * operation that should be performed on another thread,
     * {@link Result#detach result.detach} may be called before returning from
     * this function, and then {@link Result#sendResult result.sendResult}
     * called when the loading is complete.
     * </p><p>
     * In case the media item does not have any children, call {@link Result#sendResult}
     * with an empty list. When the given {@code parentId} is invalid, implementations must
     * call {@link Result#sendResult result.sendResult} with {@code null}, which will invoke
     * {@link MediaBrowser.SubscriptionCallback#onError}.
     * </p>
     *
     * @param parentId The id of the parent media item whose children are to be
     *            queried.
     * @param result The Result to send the list of children to.
     */
    public abstract void onLoadChildren(@NonNull String parentId,
            @NonNull Result<List<MediaBrowser.MediaItem>> result);

onGetRoot会在客户端发起连接时被调用,而onLoadchildren会在客户端发起订阅请求时被调用。onGetRoot方法的参数是clientPackageName和客户端的UID,我们可以针对这两个参数做一些限制,比如允许哪些客户端连接之类的,如果不允许就直接返回一个null就行了,否则就返回一个新的BrowserRoot对象。函数onLoadChildren则是在客户端发起订阅请求时被调用的,在这个函数中,我们扫描音乐文件,然后将其打包到一个list中,再返回给客户端。

回到 A2dpMediaBrowserService 服务中我们看到这两个函数的实现也是比较简单的,这个可以根据实际的业务需求来做

    @Override
    public BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints) {
        return new BrowserRoot(BrowseTree.ROOT, null);
    }

    @Override
    public synchronized void onLoadChildren(final String parentMediaId,
            final Result<List<MediaItem>> result) {
        if (mAvrcpCtrlSrvc == null) {
            Log.w(TAG, "AVRCP not yet connected.");
            result.sendResult(Collections.emptyList());
            return;
        }

        if (DBG) Log.d(TAG, "onLoadChildren parentMediaId=" + parentMediaId);
        if (!mAvrcpCtrlSrvc.getChildren(mA2dpDevice, parentMediaId, 0, 0xff)) {
            result.sendResult(Collections.emptyList());
            return;
        }

        // Since we are using this thread from a binder thread we should make sure that
        // we synchronize against other such asynchronous calls.
        synchronized (this) {
            mParentIdToRequestMap.put(parentMediaId, result);
        }
        result.detach();
    }

2、MediaSession

其实上面我们看到的MediaBrowseService其实是封装了一层逻辑的,里面主要的实现还是 MediaSession,那么接下来有必要看看 MediaSession 是怎么被使用的

还是在 A2dpMediaBrowserService 中

    @Override
    public void onCreate() {
        if (DBG) Log.d(TAG, "onCreate");
        super.onCreate();

        mSession = new MediaSession(this, TAG);
        setSessionToken(mSession.getSessionToken());
        mSession.setCallback(mSessionCallbacks);
        mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS
                | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mSession.setActive(true);
        mAvrcpCommandQueue = new AvrcpCommandQueueHandler(Looper.getMainLooper(), this);

        refreshInitialPlayingState();

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(AvrcpControllerService.ACTION_BROWSE_CONNECTION_STATE_CHANGED);
        filter.addAction(AvrcpControllerService.ACTION_TRACK_EVENT);
        filter.addAction(AvrcpControllerService.ACTION_FOLDER_LIST);
        registerReceiver(mBtReceiver, filter);

        synchronized (this) {
            mParentIdToRequestMap.clear();
        }
    }

这里初始化之后设置了 MediaSession.Callback, 当客户端MediaController发送指令时会回调到这里

    // Media Session Stuff.
    private MediaSession.Callback mSessionCallbacks = new MediaSession.Callback() {
        @Override
        public void onPlay() {
            if (DBG) Log.d(TAG, "onPlay");
            mAvrcpCommandQueue.obtainMessage(MSG_AVRCP_PASSTHRU,
                    AvrcpControllerService.PASS_THRU_CMD_ID_PLAY).sendToTarget();
            // TRACK_EVENT should be fired eventually and the UI should be hence updated.
        }

        @Override
        public void onPause() {
            if (DBG) Log.d(TAG, "onPause");
            mAvrcpCommandQueue.obtainMessage(MSG_AVRCP_PASSTHRU,
                    AvrcpControllerService.PASS_THRU_CMD_ID_PAUSE).sendToTarget();
            // TRACK_EVENT should be fired eventually and the UI should be hence updated.
        }
        // ....
    }

关于MediaSession 内部的实现可以看这篇文章

Android MediaSession简单分析 - 简书

客户端调用

1、MediaBrowser + MediaController

MediaBrowser 媒体浏览器,用来连接媒体服务MediaBrowserService和订阅数据,在注册的回调接口中我们就可以获取到Service的连接状态、获取音乐数据。一般在客户端中创建

MediaController 媒体控制器,在客户端中工作,通过控制器向媒体服务器发送指令,然后通过MediaControllerCompat.Callback设置回调函数来接受服务端的状态。MediaController创建时需要受控端的配对令牌,因此需要在浏览器连接成功后才进行

所以要监听哪个服务端需要在MediaBrowser连接服务的地方传递包名和类名, 这里使用了 MediaSessionCompat

mBrowser = new MediaBrowserCompat(MainActivity.this,
 new ComponentName(packageName,className),
 connectionCallback,null);
// 并且注册MediaControler connect callback,如果连接成功则回调

这个是注册mediacontroller的回调,

mController = new MediaControllerCompat(MainActivity.this, mBrowser.getSessionToken());
mController.registerCallback(controllerCallback);

将上面的packageName和className改成avrcp协议对应的服务就行了,但是不同的android版本对应的协议包名类名不一样 android7-9

String package = "com.android.bluetooth";
String class = "com.android.bluetooth.a2dpsink.mbs.A2dpMediaBrowserService"

android10以后

String package = "com.android.bluetooth";
String class = "com.android.bluetooth.avrcpcontroller.BluetoothMediaBrowserService"

2、MediaSessionManager + MediaController

通过SessionManager获取全部激活的session,然后编译其中获取你想要的controller

        mMediaCtrlCallback = new MediaControllerCallback();
        mSessionManager =
                (MediaSessionManager) getSystemService(MEDIA_SESSION_SERVICE);
        mSessionListener = new SessionChangeListener();
        if (mSessionManager != null) {
            mSessionManager.addOnActiveSessionsChangedListener(mSessionListener, null,
                    mHandler);
            List<MediaController> controllers = mSessionManager.getActiveSessions(null);
            for (int i = 0; i < controllers.size(); i++) {
                MediaController controller = (MediaController) controllers.get(i);
                if ((getMediaControllerTag(controller).contains(A2DP_MBS_TAG))) {
                    setCurrentMediaController(controller);
                }
            }
        }

需要上述代码中TAG名需要对上,这里看的源码是Android P,AVRCP服务端中注册的TAG是

A2dpMediaBrowserService

看MediaController 中的方法

    /**
     * Get the session owner's package name.
     *
     * @return The package name of of the session owner.
     */
    public String getPackageName() {
        if (mPackageName == null) {
            try {
                mPackageName = mSessionBinder.getPackageName();
            } catch (RemoteException e) {
                Log.d(TAG, "Dead object in getPackageName.", e);
            }
        }
        return mPackageName;
    }
    /**
     * Get the session's tag for debugging purposes.
     *
     * @return The session's tag.
     * @hide
     */
    public String getTag() {
        if (mTag == null) {
            try {
                mTag = mSessionBinder.getTag();
            } catch (RemoteException e) {
                Log.d(TAG, "Dead object in getTag.", e);
            }
        }
        return mTag;
    }

然后再向对应的controller 注册 callback来使用即可

    mMediaController.registerCallback(mMediaCtrlCallback);

mMediaCtrlCallback 实现抽象接口类如下,可以监听到播放状态和多媒体信息变化的修改

    private class MediaControllerCallback extends MediaController.Callback {
        @Override
        public void onPlaybackStateChanged(PlaybackState state) {
        }

        @Override
        public void onMetadataChanged(MediaMetadata metadata) {
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容