private MediaSession mSession = null;
private ComponentName mbCN;
private CustomMediaButtonReceiver wireControlReceiver;
/**
* 媒体按键监听,线控 相关变量初始化
*/
private void initMediaBtnReceiverConst(){
if (mbCN == null)
mbCN = new ComponentName(getPackageName(), CustomMediaButtonReceiver.class.getName());
if (wireControlReceiver == null)
wireControlReceiver = new CustomMediaButtonReceiver();
}
/**
* 媒体按键监听,线控
*/
public void registerMediaBtnReceiver(){
Context mContext = getApplicationContext();
initMediaBtnReceiverConst();
if (android.os.Build.VERSION.SDK_INT >= 21) {
if (mSession == null)
mSession = new MediaSession(mContext, "MusicService");
mSession.setCallback(new MediaSession.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
// FucUtil.logMediaKey(mContext,mediaButtonIntent,"registerMediaBtnReceiver() ");
wireControlReceiver.onReceive(mContext, mediaButtonIntent);
return super.onMediaButtonEvent(mediaButtonIntent);
}
});
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
ComponentName mediaButtonReceiverComponent = new ComponentName(mContext, WireControlReceiver.class);
mediaButtonIntent.setComponent(mediaButtonReceiverComponent);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 0,
mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
mSession.setMediaButtonReceiver(mediaPendingIntent);
AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder();
audioAttributesBuilder.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
mSession.setPlaybackToLocal(audioAttributesBuilder.build());
mSession.setActive(true);
PlaybackState state = new PlaybackState.Builder()
.setActions(
PlaybackState.ACTION_PLAY
| PlaybackState.ACTION_PLAY_PAUSE
| PlaybackState.ACTION_PLAY_FROM_MEDIA_ID
| PlaybackState.ACTION_PAUSE
| PlaybackState.ACTION_SKIP_TO_NEXT
| PlaybackState.ACTION_SKIP_TO_PREVIOUS)
.setState(PlaybackState.STATE_PLAYING, 0, 1,
SystemClock.elapsedRealtime()).build();
mSession.setPlaybackState(state);
}
////获得AudioManager对象
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.registerMediaButtonEventReceiver(mbCN);
Intent mediaButtonIntent = new Intent(
Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(mbCN);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(
mContext, 0, mediaButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
// create and register the remote control client
RemoteControlClient mRemoteControlClient = new RemoteControlClient(
mediaPendingIntent);
audioManager.registerRemoteControlClient(mRemoteControlClient);
int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
| RemoteControlClient.FLAG_KEY_MEDIA_NEXT
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY
| RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_RATING;
mRemoteControlClient
.setTransportControlFlags(flags);
IntentFilter inFilter = new IntentFilter(
"android.intent.action.MEDIA_BUTTON");
inFilter.setPriority(10000);
mContext.registerReceiver(wireControlReceiver, inFilter);
}
网友评论