0

Problem: I am trying to get my audio to continue to play even when the app is in the background. The desired result would be that the audio continues to play even when: the home button is pressed, the phone sleeps/is locked, and to be able to mix with the other sounds (native music app, etc). I have looked at the other examples and have tried implementing them but without success. After starting the app action, my app plays a short sound and then waits a period of time, which it then plays another sound.

Things I have tried -

  1. AVAudioPlayer not responding in the background
  2. How to Play Audio in Background Swift?

Things I have learned so far is that I probably want to use the AVAudioSession with the Ambient category so that my sound mixes with the native music app. I have turned on the background capabilities in the tab and in the plist. I have tried putting the AVAudioSession in multiple places (delegate/VC file).

I am using the code from #2 to start my audioSession. Here is the code that plays the sound

func playComboSound(fileName:String){
    if(fileName != "burpees" && fileName != "pushUps" && fileName != "sitUps")
    {
        let url = NSBundle.mainBundle().URLForResource(fileName, withExtension: "mp3")!
        do {
            appDelegate.audioPlayer = try AVAudioPlayer(contentsOfURL: url)
            //guard let appDelegate.audioPlayer = audioPlayer else { return }
            //appDelegate.audioPlayer!.stop()
            appDelegate.audioPlayer!.prepareToPlay()
            appDelegate.audioPlayer!.play()
        } catch let error as NSError {
        print(error.description)
    }
    }
    else
    {
        let numRepAudio: String
        switch numReps{
            case "THREE":
                numRepAudio = "three"
            case "FIVE":
                numRepAudio = "five"
            case "TEN":
                numRepAudio = "ten"
            default:
                numRepAudio = "three"
        }

        let url1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(numRepAudio, ofType: "mp3")!)
        let url2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")!)
        print(numRepAudio)
        print(fileName)
        let item1 = AVPlayerItem(URL: url1)
        let item2 = AVPlayerItem(URL: url2)
        let items: [AVPlayerItem] = [item1, item2]
        appDelegate.playerQueue = AVQueuePlayer(items: items)
        appDelegate.playerQueue!.play()
    }
}

Thank you

Community
  • 1
  • 1
Harmon023
  • 1
  • 2
  • Basically, since my app plays a string of non-continuous sounds, how do I prevent my app from being suspended? – Harmon023 Jul 05 '16 at 23:43
  • Have a look at AVComposition. In essence, you have to combine your tracks into a single composition which will then play in background. –  Jul 06 '16 at 02:09
  • Will that work even if the tracks are not played in a specific order each time? (randomly chosen) – Harmon023 Jul 06 '16 at 16:21
  • What you have to do is write code to manage all of that yourself. For example, you can keep an array of times, one for each original track, that indicates its start position in the composition. –  Jul 07 '16 at 14:35
  • Alright, ill give it a try, thank you – Harmon023 Jul 07 '16 at 16:17
  • are you still having this issue? – MikeG Nov 18 '16 at 18:59
  • Unfortunately it got put on hold, I would be interested to know if you had a solution but would rather not waste your time if it is going to cause you a lot of trouble – Harmon023 Nov 25 '16 at 15:36

0 Answers0