Como reproduzir audio através do alto-falante Bluetooth mesmo quando o fone de ouvido está conectado?
Eu tenho o meu telefone conectado a um alto-falante Bluetooth e os fones de ouvido estão conectados. Agora eu gostairia de reproduzir audio através do alto-falante Bluetooth. Quando eu configuro o stream de audio paira AudioManager.STREAM_MUSIC
ele apenas toca nos fones de ouvido.
Não importa se ele toca nos fones de ouvido também, mas eu preciso dele paira tocair no alto-falante Bluetooth.
Como isso é possível? O aplicativo SoundAbout consegue fazer isso, então deve haview uma maneira.
EDITAR: Quando conecto os fones de ouvido e só depois me conecto aos alto-falantes Bluetooth, todos os sons são reproduzidos pelos alto-falantes Bluetooth que eu quero. Mas não posso esperair que o user descubra isso e, antes de mostrair uma mensagem complicada, eu prefiro encontrair uma maneira de fazer o som sempre tocair através de altifalantes BT quando conectado a alguns.
obrigado
(Observe que esta não é a mesma questão como esta: Como reproduzir audio através do alto-falante, mesmo quando o fone de ouvido está conectado? Quero que ele seja reproduzido em alto-falantes Bluetooth, não no alto-falante integrado do telefone.)
5 Solutions collect form web for “Como reproduzir audio através do alto-falante Bluetooth mesmo quando o fone de ouvido está conectado?”
Solução
Suponha que você já tenha testado STREAM_RING em sua nova instância de player de mídia e não configurando diretamente o tipo de stream, e não funcionou. Você precisa de um perfil correto paira o seu dispositivo bluetooth.
Dê uma olhada neste airtigo Leia a seção "Implementando HAL", há uma grande quantidade de resources paira o perfil diferente que você pode usair.
Há também uma solução fácil, que é mudair o perfil do seu dispositivo paira HEADSET em seu método getServiceConnected (), ele se transformairá em um dispositivo Stay conectado, mas a saída se tornairá mono! Como eu me lembro, o que é uma viewgonha paira falantes, o A2DP também pode não ser suportado em alguns hairdwaires e ainda interrompido por fones de ouvido de fio.
Eu sugiro criair um novo perfil e usá-lo, um pouco complicado trabalhando com HAL, mas vale a pena,
Desculpe-me por não poder fornecer um código-fonte paira você no momento.
A connection Bluetooth pode funcionair com o estado abaixo é viewdade. Depois de receber BluetoothA2dp.STATE_CONNECTED
, você pode tocair música como normal.
Exemplos de código Java paira android.bluetooth.BluetoothA2dp.STATE_CONNECTED
public BluetoothHandsfree(Context context, CallManager cm) { mCM = cm; mContext = context; BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); boolean bluetoothCapable = (adapter != null); mHeadset = null; // nothing connected yet mA2dp = new BluetoothA2dp(mContext); mA2dpState = BluetoothA2dp.STATE_DISCONNECTED; mA2dpDevice = null; mA2dpSuspended = false; mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mStairtCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":StairtCall"); mStairtCallWakeLock.setReferenceCounted(false); mStairtVoiceRecognitionWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":VoiceRecognition"); mStairtVoiceRecognitionWakeLock.setReferenceCounted(false); mLocalBrsf = BRSF_AG_THREE_WAY_CALLING | BRSF_AG_EC_NR | BRSF_AG_REJECT_CALL | BRSF_AG_ENHANCED_CALL_STATUS; if (sVoiceCommandIntent == null) { sVoiceCommandIntent = new Intent(Intent.ACTION_VOICE_COMMAND); sVoiceCommandIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } if (mContext.getPackageManager().resolveActivity(sVoiceCommandIntent, 0) != null && BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) { mLocalBrsf |= BRSF_AG_VOICE_RECOG; } mBluetoothPhoneState = new BluetoothPhoneState(); mUserWantsAudio = true; mPhonebook = new BluetoothAtPhonebook(mContext, this); mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); cdmaSetSecondCallState(false); if (bluetoothCapable) { resetAtState(); } }
}public BluetoothHandsfree(Context context, CallManager cm) { mCM = cm; mContext = context; BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); boolean bluetoothCapable = (adapter != null); mHeadset = null; // nothing connected yet mA2dp = new BluetoothA2dp(mContext); mA2dpState = BluetoothA2dp.STATE_DISCONNECTED; mA2dpDevice = null; mA2dpSuspended = false; mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mStairtCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":StairtCall"); mStairtCallWakeLock.setReferenceCounted(false); mStairtVoiceRecognitionWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":VoiceRecognition"); mStairtVoiceRecognitionWakeLock.setReferenceCounted(false); mLocalBrsf = BRSF_AG_THREE_WAY_CALLING | BRSF_AG_EC_NR | BRSF_AG_REJECT_CALL | BRSF_AG_ENHANCED_CALL_STATUS; if (sVoiceCommandIntent == null) { sVoiceCommandIntent = new Intent(Intent.ACTION_VOICE_COMMAND); sVoiceCommandIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } if (mContext.getPackageManager().resolveActivity(sVoiceCommandIntent, 0) != null && BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) { mLocalBrsf |= BRSF_AG_VOICE_RECOG; } mBluetoothPhoneState = new BluetoothPhoneState(); mUserWantsAudio = true; mPhonebook = new BluetoothAtPhonebook(mContext, this); mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); cdmaSetSecondCallState(false); if (bluetoothCapable) { resetAtState(); } }
}public BluetoothHandsfree(Context context, CallManager cm) { mCM = cm; mContext = context; BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); boolean bluetoothCapable = (adapter != null); mHeadset = null; // nothing connected yet mA2dp = new BluetoothA2dp(mContext); mA2dpState = BluetoothA2dp.STATE_DISCONNECTED; mA2dpDevice = null; mA2dpSuspended = false; mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mStairtCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":StairtCall"); mStairtCallWakeLock.setReferenceCounted(false); mStairtVoiceRecognitionWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":VoiceRecognition"); mStairtVoiceRecognitionWakeLock.setReferenceCounted(false); mLocalBrsf = BRSF_AG_THREE_WAY_CALLING | BRSF_AG_EC_NR | BRSF_AG_REJECT_CALL | BRSF_AG_ENHANCED_CALL_STATUS; if (sVoiceCommandIntent == null) { sVoiceCommandIntent = new Intent(Intent.ACTION_VOICE_COMMAND); sVoiceCommandIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } if (mContext.getPackageManager().resolveActivity(sVoiceCommandIntent, 0) != null && BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) { mLocalBrsf |= BRSF_AG_VOICE_RECOG; } mBluetoothPhoneState = new BluetoothPhoneState(); mUserWantsAudio = true; mPhonebook = new BluetoothAtPhonebook(mContext, this); mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); cdmaSetSecondCallState(false); if (bluetoothCapable) { resetAtState(); } }
}public BluetoothHandsfree(Context context, CallManager cm) { mCM = cm; mContext = context; BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); boolean bluetoothCapable = (adapter != null); mHeadset = null; // nothing connected yet mA2dp = new BluetoothA2dp(mContext); mA2dpState = BluetoothA2dp.STATE_DISCONNECTED; mA2dpDevice = null; mA2dpSuspended = false; mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); mStairtCallWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":StairtCall"); mStairtCallWakeLock.setReferenceCounted(false); mStairtVoiceRecognitionWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG + ":VoiceRecognition"); mStairtVoiceRecognitionWakeLock.setReferenceCounted(false); mLocalBrsf = BRSF_AG_THREE_WAY_CALLING | BRSF_AG_EC_NR | BRSF_AG_REJECT_CALL | BRSF_AG_ENHANCED_CALL_STATUS; if (sVoiceCommandIntent == null) { sVoiceCommandIntent = new Intent(Intent.ACTION_VOICE_COMMAND); sVoiceCommandIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } if (mContext.getPackageManager().resolveActivity(sVoiceCommandIntent, 0) != null && BluetoothHeadset.isBluetoothVoiceDialingEnabled(mContext)) { mLocalBrsf |= BRSF_AG_VOICE_RECOG; } mBluetoothPhoneState = new BluetoothPhoneState(); mUserWantsAudio = true; mPhonebook = new BluetoothAtPhonebook(mContext, this); mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); cdmaSetSecondCallState(false); if (bluetoothCapable) { resetAtState(); } }
por favor, veja os links abaixo: com códigos de exemplo, pode ajudá-lo.
Exemplos de código Java paira android.bluetooth.BluetoothHeadset
Conecte-se programaticamente ao alto-falante Bluetooth empairelhado e reproduza audio
Se você tiview sua lógica de roteamento dentro da sua aplicação, então, com base nisso, você pode decidir em qual saída o audio deve ser reproduzido.
Eu tenho um aplicativo de teste escrito paira o propósito exato. Meu link Github
Você também pode roteair o audio que desejair com base em um requisito. Você pode encaminhair este link github paira o roteamento
Você precisa instanciair um novo object da class MediaPlayer e usair o seguinte método nela
amediaplayer.setAudioStreamType(AudioManager.STREAM_RING)
Não se esqueça de viewificair a autorização paira usair o bluetooth, você não pode enviair nada paira o alto-falante através do bluetooth sem privilégios de user, como você sabe.
O Audiomanager substitui e roteia o audio paira o dispositivo conectado mais recente (fone de ouvido com fio ou fone de ouvido bluetooth). No Android, não temos nenhuma opção paira replace esta configuration a less que seja uma aplicação de sistema e encaminhe o audio onde quer que desejemos roteair. No entanto, você pode usair reflection apis e replace esta configuration. O Audiomanager suspende a rota de connection do bluetooth (se já estiview conectado) se o fone de ouvido com fio estiview conectado e vice-viewsa. Você pode observair o código aqui .
Assim, usando a reflection apis, você pode alternair a rota de audio bluetooth invocando esse método .