Play Webrtc Audio message when IOS app is in background

520 Views Asked by At

Hi this might be duplicate to this question. But I did not find right answer for this. I have developed an app like zello push to talk using react-native. I want to play the audio message automatically without any user interaction when the app is in background or killed. Whenever any user sending real time audio message, I am sending a push notification to ios app and after receiving notification I am invoking a function which establishes socket connection with the webrtc server and then join in the room in which webrtc audio broadcasting is happening. Now I checked that socket is connected and also joining in room successfully done after receiving push notification when app is in background. But there I could not hear any audio and received following error message.

AURemoteIO.cpp:1668  AUIOClient_StartIO failed (561145187)

Then I have set AVAudioSessionCategoryPlayback and that error gone. But this time also did not hear any audio sound. The app is working fine when it is in foreground. I am using react-native and this is happening for IOS app only. Any help is appreciated. In xcode I have enabled push-notifications, background-fetch, background-airplay etc.

1

There are 1 best solutions below

3
Daljeet On

You need to set your app Capabilities Background Modes (Audio and AirPlay).

To Enable this select your iOS project in iOS then go to Signing & Capabilities tab.

Check for Background Modes and select Audio, AirPlay and Picture In Picture option. Also, set your AVAudioSession category to AVAudioSessionCategoryPlayback and set it active. Example:

do {
      try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: AVAudioSession.CategoryOptions.mixWithOthers)
      NSLog("Playback OK")
      try AVAudioSession.sharedInstance().setActive(true)
       NSLog("Session is Active")
     } catch {
       NSLog("ERROR: CANNOT PLAY MUSIC IN BACKGROUND. Message from code: \"\(error)\"")
  }