0

My iOS app receives notification to refresh its state from our main service. Right now, we are fetching the latest state and we play a continues sound when there is updated data. Our devices are locked in guided mode to stop them from turning off.

We are making changes such that the device can go to sleep after xx minutes of inactivity. However, we are noticing that the sound doesn't play when the app is in background and it always seems to fail on AVAudioSession.sharedInstance().setActive(true). Interestingly, if put breakpoints and run it through the debugger, it works fine but when running normally, it fails with this error:

Error Domain=NSOSStatusErrorDomain Code=561015905

We have the "Audio, AirPlay, and PnP" enabled under background modes under capabilities. Here is the code for playing the sound:

func playSound(shouldPlay: Bool) {
        guard let url = Bundle.main.url(forResource: "sms_alert_circles", withExtension: "caf") else { return }

        let audioSession = AVAudioSession.sharedInstance()

            do {
                try self.audioSession.setCategory(.playback, mode: .default, options: .mixWithOthers)
                try self.audioSession.setActive(true)

                /* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
                self.player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.caf.rawValue)

                guard let player = self.player else { return }

                if shouldPlay == true {
                    player.volume = 1.0
                    player.numberOfLoops = 1
                    player.play()
                } else {
                    player.stop()
                    try self.audioSession.setActive(false)
                }

            } catch let error {
                print("Error playing sounds")
                print(error.localizedDescription)
            }
        
    }

I am hoping someone can point out the issue.

whawhat
  • 175
  • 2
  • 7
  • Unclear from your description, but you do understand, don't you, that even the background mode capability does not give you the ability to _start_ playing a sound when you are _already_ in the background. – matt Jun 27 '20 at 01:35
  • It is also not clear under which class you are running this audio show. – El Tomato Jun 27 '20 at 01:41
  • @matt: If I add a sleep(1) before ```self.audioSession.setActive(true)```, it works fine. I am confused when you say that I can't play sound while in background since I have seen these posts on SO: https://stackoverflow.com/questions/30280519/. – whawhat Jun 27 '20 at 15:03
  • Well, adding `sleep(1)` is illegal, but wrapping everything in `asyncAfter` is legal, so why not do that? – matt Jun 27 '20 at 15:47
  • Sleep was added as a troubleshooting step. It will not be included in our final app. I will try the ```asyncafter``` approach and report back. Thank you. – whawhat Jun 27 '20 at 16:13
  • Seeing some interesting behavior. On first notification, sound doesn't play but if another notification is sent (either after few seconds or after few minutes), the sound starts playing even though the app is running in the background. – whawhat Jun 27 '20 at 16:24

0 Answers0