测试机
- 荣耀9x(Android 10)
- 三星(Android 7)
开发环境
- Android Studio 3.5.1
- compileSdkVersion 30
- buildToolsVersion “30.0.1”
- minSdkVersion 21
- targetSdkVersion 30
播放音乐,不先播放音乐会被别的音频播放器把广播给拦截。
下面播放Raw目录下的音频文件。
mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.hui);
try {mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IOException e) {e.printStackTrace();
}
//异步准备播放
mediaPlayer.prepareAsync();
//设置准备就绪状态监听
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {@Overridepublic void onPrepared(MediaPlayer mp) {// 开始播放mediaPlayer.start();}
});
按键状态监听
Api21 之前
- 新建按键广播监听类
/*** Author: jVen* Time: 2020/8/14 16:39* Description:*/
public class MyMediaButtonReceiver extends BroadcastReceiver {private String TAG = "MyMediaButtonReceiver";@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action != null) {if (TextUtils.equals(action, Intent.ACTION_MEDIA_BUTTON)) {KeyEvent keyEvent = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);if (keyEvent != null) {if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {int keyCode = keyEvent.getKeyCode();switch (keyCode) {//可以通过广播形式通知Activity更新UIcase KeyEvent.KEYCODE_MEDIA_PLAY:Toast.makeText(context, "onReceive: 播放", Toast.LENGTH_SHORT).show();break;case KeyEvent.KEYCODE_MEDIA_PAUSE:Toast.makeText(context, "onReceive: 暂停", Toast.LENGTH_SHORT).show();break;case KeyEvent.KEYCODE_MEDIA_NEXT:Toast.makeText(context, "onReceive: 下一首", Toast.LENGTH_SHORT).show();break;case KeyEvent.KEYCODE_MEDIA_PREVIOUS:Toast.makeText(context, "onReceive: 上一首", Toast.LENGTH_SHORT).show();break;}}}}}}
}
- AndroidManifest.xml
<receiver android:name=".MyMediaButtonReceiver"><intent-filter><action android:name="android.intent.action.MEDIA_BUTTON" /></intent-filter>
</receiver>
- Activity注册广播
//获取音频服务
AudioManager audioManager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
//注册接收的Receiver
ComponentName mRemoteControlClientReceiverComponent;
mRemoteControlClientReceiverComponent = new ComponentName(getPackageName(), MyMediaButtonReceiver.class.getName());
//注册MediaButton
audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
registerMediaButtonEventReceiver Api21之后过时,但是两部测试机依然可以使用这个监听到蓝牙的按键事件。
Api21之后
- 添加依赖
implementation "androidx.media:media:1.1.0"
- 注册广播
ComponentName mbr = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
mMediaSession = new MediaSessionCompat(this, "mbr", mbr, null);
//一定要设置
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
if (!mMediaSession.isActive()) {mMediaSession.setActive(true);
}
mMediaSession.setCallback(new MediaSessionCompat.Callback() {@Overridepublic boolean onMediaButtonEvent(Intent intent) {String action = intent.getAction();if (action != null) {if (TextUtils.equals(action, Intent.ACTION_MEDIA_BUTTON)) {KeyEvent keyEvent = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);if (keyEvent != null) {if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {int keyCode = keyEvent.getKeyCode();switch (keyCode) {//可以通过广播形式通知Activity更新UIcase KeyEvent.KEYCODE_MEDIA_PLAY:Toast.makeText(MainActivity.this, "onReceive: 播放", Toast.LENGTH_SHORT).show();break;case KeyEvent.KEYCODE_MEDIA_PAUSE:Toast.makeText(MainActivity.this, "onReceive: 暂停", Toast.LENGTH_SHORT).show();break;case KeyEvent.KEYCODE_MEDIA_NEXT:Toast.makeText(MainActivity.this, "onReceive: 下一首", Toast.LENGTH_SHORT).show();break;case KeyEvent.KEYCODE_MEDIA_PREVIOUS:Toast.makeText(MainActivity.this, "onReceive: 上一首", Toast.LENGTH_SHORT).show();break;}}}}}return super.onMediaButtonEvent(intent);}
});
一定要先播放音乐事件才不会被别的播放器拦截,最好把音频焦点也获取了(焦点获取了别的播放器的逻辑里面会暂停播放)