一:客户端
1:MediaBrowserCompat用来连接服务端,通过服务端的ComponentName即可达到,连接成功以后,即可创建MediaControllerCompat了:
MediaBrowserCompat mMediaBrowser = new MediaBrowserCompat(mContext,
mServiceInfo.componentName, new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnected() {
connectToSession(mMediaBrowser.getSessionToken());
}
@Override
public void onConnectionSuspended() {
GwmLog.d(TAG, "onConnectionSuspended");
connectMediaService();
}
@Override
public void onConnectionFailed() {
GwmLog.d(TAG, "onConnectionFailed");
disConnectMediaService();
}
}, null);
mMediaBrowser.connect();
2:创建MediaControllerCompat对象,MediaControllerCompat不仅可以通过注册callback收取服务端下发的指令,还可以向服务端发送指令作用非常大:
private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
MediaControllerCompat mMediaController = new MediaControllerCompat(mContext,
token);
mMediaController.registerCallback(mControllerCallback);
}
向服务端发送指令可以是:
mMediaController.getTransportControls().sendCustomAction(action, args);
mMediaController.sendCommand(command, params, receiver);
以上为两种不同的发送指令的方式,当然服务端接收的时候也是通过不同的方法
3:calback可以获取到服务端下发的指令:
private final MediaControllerCompat.Callback mControllerCallback = new
MediaControllerCompat.Callback() {
@Override
public void onSessionReady() {
Log.d(TAG, "onSessionReady");
}
@Override
public void onSessionDestroyed() {
Log.d(TAG, "onSessionDestroyed: ");
}
@Override
public void onSessionEvent(String event, Bundle extras) {
sendSessionEvent(event, displayId, extras);
}
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
notifyPlaybackStateChanged(state, displayId);
}
@Override
public void onMetadataChanged(MediaMetadataCompat metadata) {
notifyMetaDataChanged(metadata, displayId);
}
@Override
public void onQueueChanged(List<MediaSessionCompat.QueueItem> queue) {
notifyQueueChanged(queue, bundle, displayId);
}
};
二:服务端
1:继承 MediaBrowserServiceCompat
public class MediaBluetoothService extends MediaBrowserServiceCompat
2:创建MediaSession,通过MediaSession获取客户端传过来的指令对应关系如下:
sendCustomAction→onCustomAction
sendCommand→onCommand
@Override
public void onCreate() {
super.onCreate();
MediaSessionCompat mMediaSession = new MediaSessionCompat(this, TAG);
setSessionToken(mMediaSession.getSessionToken());
mMediaSession.setCallback(mServiceSessionCallback);
}
private class CoreServiceSessionCallback extends MediaSessionCompat.Callback {
@Override
public void onCustomAction(String action, Bundle extras) {
mBluetoothPlayer.sendCustomAction(extras);
}
@Override
public void onCommand(String command, Bundle extras, ResultReceiver cb) {
switch (command) {
case CommandActionId.CONTROLLER_COMMAND_MEDIA_PLAY:
mBluetoothPlayer.play();
break;
case CommandActionId.CONTROLLER_COMMAND_MEDIA_PAUSE:
mBluetoothPlayer.pause();
break;
case CommandActionId.CONTROLLER_COMMAND_MEDIA_SKIP_TO_NEXT:
mBluetoothPlayer.next();
break;
case CommandActionId.CONTROLLER_COMMAND_MEDIA_SKIP_TO_PREVIOUS:
mBluetoothPlayer.prev();
break;
case CommandActionId.CONTROLLER_COMMAND_SYNC_PLAYING_INFO:
mBluetoothPlayer.syncPlayingInfo();
default:
break;
}
}
@Override
public boolean onMediaButtonEvent(@androidx.annotation.NonNull Intent
mediaButtonEvent) {
sendMediaButtonEvent(mediaButtonEvent);
return true;
}
private void sendMediaButtonEvent(Intent mediaButtonEvent) {
Bundle bundle = new Bundle();
bundle.putParcelable(BundleKeys.BUNDLE_DATA, mediaButtonEvent);
mMediaSession.sendSessionEvent(SESSION_EVENT_MEDIA_BUTTON_EVENT, bundle);
}
}
3:服务端发送到客户端,直接通过mMediaSession即可操作
mMediaSession.setMetadata(builder.build());
mMediaSession.sendSessionEvent(PLAYER_ACTIVE_EVENT, bundle);
mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_NONE, 0, 0).setExtras(bundle).build());
感兴趣的小伙伴可以去客户端的回调里去找,肯定是可以对的上的,以上只是我对MediaSession最浅显的认知,不足之处望求指正