17
17
import android .content .Intent ;
18
18
import android .content .IntentFilter ;
19
19
import android .media .AudioAttributes ;
20
+ import android .media .AudioDeviceInfo ;
20
21
import android .media .AudioFocusRequest ;
21
22
import android .media .AudioManager ;
22
23
import android .os .Build ;
@@ -417,6 +418,7 @@ public void onHostDestroy() {
417
418
418
419
if (localAudioTrack != null ) {
419
420
localAudioTrack .release ();
421
+ audioManager .stopBluetoothSco ();
420
422
localAudioTrack = null ;
421
423
}
422
424
@@ -553,6 +555,29 @@ public void connectToRoom() {
553
555
room = Video .connect (getContext (), connectOptionsBuilder .build (), roomListener ());
554
556
}
555
557
558
+ public void setAudioType () {
559
+ AudioDeviceInfo [] devicesInfo = audioManager .getDevices (AudioManager .GET_DEVICES_OUTPUTS );
560
+ boolean hasNonSpeakerphoneDevice = false ;
561
+ for (int i = 0 ; i < devicesInfo .length ; i ++) {
562
+ int deviceType = devicesInfo [i ].getType ();
563
+ if (
564
+ deviceType == AudioDeviceInfo .TYPE_WIRED_HEADSET ||
565
+ deviceType == AudioDeviceInfo .TYPE_WIRED_HEADPHONES
566
+ ) {
567
+ hasNonSpeakerphoneDevice = true ;
568
+ }
569
+ if (
570
+ deviceType == AudioDeviceInfo .TYPE_BLUETOOTH_A2DP ||
571
+ deviceType == AudioDeviceInfo .TYPE_BLUETOOTH_SCO
572
+ ) {
573
+ audioManager .startBluetoothSco ();
574
+ audioManager .setBluetoothScoOn (true );
575
+ hasNonSpeakerphoneDevice = true ;
576
+ }
577
+ }
578
+ audioManager .setSpeakerphoneOn (!hasNonSpeakerphoneDevice );
579
+ }
580
+
556
581
private void setAudioFocus (boolean focus ) {
557
582
if (focus ) {
558
583
previousAudioMode = audioManager .getMode ();
@@ -581,7 +606,7 @@ private void setAudioFocus(boolean focus) {
581
606
* speaker mode if this is not set.
582
607
*/
583
608
audioManager .setMode (AudioManager .MODE_IN_COMMUNICATION );
584
- audioManager . setSpeakerphoneOn (! audioManager . isWiredHeadsetOn () );
609
+ setAudioType ( );
585
610
getContext ().registerReceiver (myNoisyAudioStreamReceiver , intentFilter );
586
611
587
612
} else {
@@ -610,7 +635,7 @@ private class BecomingNoisyReceiver extends BroadcastReceiver {
610
635
public void onReceive (Context context , Intent intent ) {
611
636
// audioManager.setSpeakerphoneOn(true);
612
637
if (Intent .ACTION_HEADSET_PLUG .equals (intent .getAction ())) {
613
- audioManager . setSpeakerphoneOn (! audioManager . isWiredHeadsetOn () );
638
+ setAudioType ( );
614
639
}
615
640
}
616
641
}
@@ -629,10 +654,12 @@ public void disconnect() {
629
654
if (localAudioTrack != null ) {
630
655
localAudioTrack .release ();
631
656
localAudioTrack = null ;
657
+ audioManager .stopBluetoothSco ();
632
658
}
633
659
if (localVideoTrack != null ) {
634
660
localVideoTrack .release ();
635
661
localVideoTrack = null ;
662
+ audioManager .stopBluetoothSco ();
636
663
}
637
664
setAudioFocus (false );
638
665
if (cameraCapturer != null ) {
0 commit comments