5
votes

I am getting the following error:

[0x19bf89310] AVAudioSession.mm:646: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

I have read through this post:

however the solution there did not solve my problem. which is:

  • small application (game)
  • audio background music handled in AudioHelper.swift
  • additional 'blip' sounds played with SKActino.playSoundFile

reproduction

  • start game (background music starts to play)
  • close game (music stops)
  • restart game (music starts again)
  • error above appears
  • SKAction.playSoundFile don't always work anymore (some do, some don't)

my audio handling code (aka AudioHelper.swift)

class AudioHelper: NSObject, AVAudioPlayerDelegate {
    var player : AVAudioPlayer?

    class var defaultHelper : AudioHelper {
        struct Static {
            static let instance : AudioHelper = AudioHelper()
        }

        return Static.instance
    }

    override init() {
        super.init()
    }

    func initializeAudio() {
        var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound_float", ofType: ".mp3")!)
        self.player = AVAudioPlayer(contentsOfURL: url, error: nil)
        self.player?.numberOfLoops = -1
        self.player?.play()
    }

    func stopAudio() {
        self.player?.stop()
        self.player?.prepareToPlay()
        AVAudioSession.sharedInstance().setActive(false, error: nil)
    }

    func startAudio() {
        AVAudioSession.sharedInstance().setActive(true, error: nil)
        self.player?.play()
    }

    func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
        self.stopAudio()
    }
}

I am initializing & pausing & starting through the AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        AudioHelper.defaultHelper.initializeAudio()
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
        AudioHelper.defaultHelper.stopAudio()
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        AudioHelper.defaultHelper.startAudio()
    }
}

where am I going wrong?

1
Just a tip - create managers for these things. You're breaking a lot of standards and losing readability by having all of that code in the AppDelegate.kpsharp
@kpsharp moved everything to a helper. still getting the same error - see updated code above :/Sebastian Flückiger
figured it out - had to move the setActive(false). now it works (see own answer). thank you for looking into it @kpsharpSebastian Flückiger
I never thought swapping it to a manager would fix it, I just noticed that was one thing you should change. Well done, man. It's good to see that you didn't just give up on it and kept searching for an answer on your own. We need more people like you.kpsharp
Also, accept your own answer for people to see. Thanks.kpsharp

1 Answers

5
votes

i had to move the setActive(false) to the delegate method. new helper class looks like this:

import Foundation
import AVFoundation

class AudioHelper: NSObject, AVAudioPlayerDelegate {
    var player : AVAudioPlayer?

    
    class var defaultHelper : AudioHelper {
        struct Static {
            static let instance : AudioHelper = AudioHelper()
        }
        
        return Static.instance
    }
    
    override init() {
        super.init()
    }
    
    func initializeAudio() {
        var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound_float", ofType: ".mp3")!)
        self.player = AVAudioPlayer(contentsOfURL: url, error: nil)
        self.player?.numberOfLoops = -1
        self.player?.play()

    }
    
    func stopAudio() {
        self.player?.stop()
        self.player?.prepareToPlay()
    }
    
    func startAudio() {
        AVAudioSession.sharedInstance().setActive(true, error: nil)
        self.player?.play()
    }
    
    
    func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool) {
        AVAudioSession.sharedInstance().setActive(false, error: nil)
    }
}

works now