@@ -45,8 +45,8 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
4545 var audioSessionMediaServicesWereResetObserver : NSObjectProtocol ?
4646 var audioSessionSilenceSecondaryAudioHintObserver : NSObjectProtocol ?
4747
48- var defaultAudioMode : String = AVAudioSessionModeVoiceChat
49- var defaultAudioCategory : String = AVAudioSessionCategoryPlayAndRecord
48+ var incallAudioMode : String = AVAudioSessionModeVoiceChat
49+ var incallAudioCategory : String = AVAudioSessionCategoryPlayAndRecord
5050 var origAudioCategory : String !
5151 var origAudioMode : String !
5252 var audioSessionInitialized : Bool = false
@@ -55,7 +55,12 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
5555 var recordPermission : String !
5656 var cameraPermission : String !
5757 var media : String = " audio "
58-
58+
59+ // --- AVAudioSessionCategoryOptionAllowBluetooth:
60+ // --- Valid only if the audio session category is AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryRecord.
61+ // --- Using VoiceChat/VideoChat mode has the side effect of enabling the AVAudioSessionCategoryOptionAllowBluetooth category option.
62+ // --- So basically, we don't have to add AllowBluetooth options by hand.
63+
5964 //@objc func initWithBridge(_bridge: RCTBridge) {
6065 //self.bridge = _bridge
6166 override init ( ) {
@@ -80,17 +85,17 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
8085
8186 // --- auto is always true on ios
8287 if self . media == " video " {
83- self . defaultAudioMode = AVAudioSessionModeVideoChat
88+ self . incallAudioMode = AVAudioSessionModeVideoChat
8489 } else {
85- self . defaultAudioMode = AVAudioSessionModeVoiceChat
90+ self . incallAudioMode = AVAudioSessionModeVoiceChat
8691 }
87- NSLog ( " RNInCallManager.start() start InCallManager. media= \( self . media) , type= \( self . media) , mode= \( self . defaultAudioMode ) " )
92+ NSLog ( " RNInCallManager.start() start InCallManager. media= \( self . media) , type= \( self . media) , mode= \( self . incallAudioMode ) " )
8893 self . storeOriginalAudioSetup ( )
8994 self . forceSpeakerOn = 0 ;
9095 self . startAudioSessionNotification ( )
91- //self.audioSessionSetCategory(self.defaultAudioCategory , [.DefaultToSpeaker, .AllowBluetooth], #function)
92- self . audioSessionSetCategory ( self . defaultAudioCategory , nil , #function)
93- self . audioSessionSetMode ( self . defaultAudioMode , #function)
96+ //self.audioSessionSetCategory(self.incallAudioCategory , [.DefaultToSpeaker, .AllowBluetooth], #function)
97+ self . audioSessionSetCategory ( self . incallAudioCategory , nil , #function)
98+ self . audioSessionSetMode ( self . incallAudioMode , #function)
9499 self . audioSessionSetActive ( true , nil , #function)
95100 if !( ringbackUriType ?? " " ) . isEmpty {
96101 NSLog ( " RNInCallManager.start() play ringback first. type= \( ringbackUriType) " )
@@ -551,9 +556,9 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
551556 self . mRingback. numberOfLoops = - 1 // you need to stop it explicitly
552557 self . mRingback. prepareToPlay ( )
553558
554- //self.audioSessionSetCategory(self.defaultAudioCategory , [.DefaultToSpeaker, .AllowBluetooth], #function)
555- self . audioSessionSetCategory ( self . defaultAudioCategory , nil , #function)
556- self . audioSessionSetMode ( self . defaultAudioMode , #function)
559+ //self.audioSessionSetCategory(self.incallAudioCategory , [.DefaultToSpeaker, .AllowBluetooth], #function)
560+ self . audioSessionSetCategory ( self . incallAudioCategory , nil , #function)
561+ self . audioSessionSetMode ( self . incallAudioMode , #function)
557562 self . mRingback. play ( )
558563 } catch let err {
559564 NSLog ( " RNInCallManager.startRingback(): caught error= \( err) " )
@@ -597,9 +602,9 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
597602 self . mBusytone. numberOfLoops = 0 // it's part of start(), will stop at stop()
598603 self . mBusytone. prepareToPlay ( )
599604
600- //self.audioSessionSetCategory(self.defaultAudioCategory , [.DefaultToSpeaker, .AllowBluetooth], #function)
601- self . audioSessionSetCategory ( self . defaultAudioCategory , nil , #function)
602- self . audioSessionSetMode ( self . defaultAudioMode , #function)
605+ //self.audioSessionSetCategory(self.incallAudioCategory , [.DefaultToSpeaker, .AllowBluetooth], #function)
606+ self . audioSessionSetCategory ( self . incallAudioCategory , nil , #function)
607+ self . audioSessionSetMode ( self . incallAudioMode , #function)
603608 self . mBusytone. play ( )
604609 } catch let err {
605610 NSLog ( " RNInCallManager.startBusytone(): caught error= \( err) " )
@@ -617,7 +622,7 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
617622 }
618623
619624 // --- ringtoneUriType May be empty
620- @objc func startRingtone( ringtoneUriType: String ) -> Void {
625+ @objc func startRingtone( ringtoneUriType: String , ringtoneCategory : String ) -> Void {
621626 // you may rejected by apple when publish app if you use system sound instead of bundled sound.
622627 NSLog ( " RNInCallManager.startRingtone(): type= \( ringtoneUriType) " )
623628 do {
@@ -642,10 +647,19 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
642647 self . mRingtone. numberOfLoops = - 1 // you need to stop it explicitly
643648 self . mRingtone. prepareToPlay ( )
644649
645- // --- we must use playback to support background playing.
646- // --- make sure you have enabled 'audio' tag ( or 'voip' tag ) at XCode -> Capabilities -> BackgroundMode
650+ // --- 1. if we use Playback, it can supports background playing (starting from foreground), but it would not obey Ring/Silent switch.
651+ // --- make sure you have enabled 'audio' tag ( or 'voip' tag ) at XCode -> Capabilities -> BackgroundMode
652+ // --- 2. if we use SoloAmbient, it would obey Ring/Silent switch in the foreground, but does not support background playing,
653+ // --- thus, then you should play ringtone again via local notification after back to home during a ring session.
654+
655+ // we prefer 2. by default, since most of users doesn't want to interrupted by a ringtone if Silent mode is on.
656+
647657 //self.audioSessionSetCategory(AVAudioSessionCategoryPlayback, [.DuckOthers], #function)
648- self . audioSessionSetCategory ( AVAudioSessionCategoryPlayback, nil , #function)
658+ if ringtoneCategory == " playback " {
659+ self . audioSessionSetCategory ( AVAudioSessionCategoryPlayback, nil , #function)
660+ } else {
661+ self . audioSessionSetCategory ( AVAudioSessionCategorySoloAmbient, nil , #function)
662+ }
649663 self . audioSessionSetMode ( AVAudioSessionModeDefault, #function)
650664 //self.audioSessionSetActive(true, nil, #function)
651665 self . mRingtone. play ( )
0 commit comments