我正在开发一个应用程序,其目的是通过蓝牙播放音乐并处理媒体按钮。我已经成功地使用这样的代码实现了:
private void startSession() {
if(mAudioManager == null) {
mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
}
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
ComponentName eventReceiver = new ComponentName(getPackageName(), BluetoothMediaButtonReceiver.class.getName());
mAudioManager.registerMediaButtonEventReceiver(eventReceiver);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(eventReceiver);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if(mMediaSessionCompat != null) {
mMediaSessionCompat.release();
}
mMediaSessionCompat = new MediaSessionCompat(this, "PlayerServiceMediaSession", eventReceiver, mediaPendingIntent);
mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSessionCompat.setMediaButtonReceiver(mediaPendingIntent);
mMediaSessionCompat.setActive(true);
} else {
if(mMediaSession != null) {
mMediaSession.release();
}
mMediaSession = new MediaSession(this, "PlayerServiceMediaSession");
mMediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setActive(true);
mMediaSession.setMediaButtonReceiver(mediaPendingIntent);
}
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
要播放音乐,我正在使用服务,要处理按钮按下,我正在使用接收器:
public class BluetoothMediaButtonReceiver extends BroadcastReceiver {
public final static String ACTION_BUTTON_PREVIOUS = "media.button.clicked";
public final static String ACTION_BUTTON_NEXT = "media.button.nex";
public final static String ACTION_BUTTON_PLAY = "media.button.play";
public final static String ACTION_BUTTON_PAUSE = "media.button.pause";
public final static String ACTION_BUTTON_PLAY_PAUSE = "media.button.play_pause";
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
// code for fast forward
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
//Toast.makeText(context, "next button was clicked", Toast.LENGTH_SHORT).show();
context.sendBroadcast(new Intent(ACTION_BUTTON_NEXT));
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
context.sendBroadcast(new Intent(ACTION_BUTTON_PLAY_PAUSE));
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
context.sendBroadcast(new Intent(ACTION_BUTTON_PLAY));
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
context.sendBroadcast(new Intent(ACTION_BUTTON_PAUSE));
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
context.sendBroadcast(new Intent(ACTION_BUTTON_PREVIOUS));
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
// code for rewind
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
// code for stop
break;
}
}
abortBroadcast();
}
}
我正在更改元数据onMediaPlayerStart
和此类标准的东西,一切都很好,但这是问题所在:
连接到汽车音响系统并播放音乐后 - 例如,汽车音响模式停留在 FM 收音机,如果它在那里,我必须手动将其更改为车内的蓝牙
我想要实现的目标:当我的应用开始播放曲目时,我想以编程方式将汽车音频模式从 FM 收音机更改为蓝牙
我已经用谷歌搜索并在堆栈上搜索过,但在这种情况下什么都没有,我什至不确定哪个班级应该做这样的事情。我知道这个问题可能被视为一个不好的问题并且没有重点,但这是因为我对在 Android 上使用蓝牙的知识也很严密,很难找到一些关于这个问题的例子