AVAudioSession 重新激活问题

Seb*_*ger 5 avaudioplayer ios avaudiosession sprite-kit swift

我收到以下错误:

[0x19bf89310] AVAudioSession.mm:646: -[AVAudioSession setActive:withOptions:error:]: 停用正在运行 I/O 的音频会话。在停用音频会话之前,应停止或暂停所有 I/O。

我已经阅读了这篇文章:

但是那里的解决方案并没有解决我的问题。即:

  • 小应用(游戏)
  • 在 AudioHelper.swift 中处理的音频背景音乐
  • 额外的“blip”声音播放 SKActino.playSoundFile

再生产

  • 开始游戏(背景音乐开始播放)
  • 关闭游戏(音乐停止)
  • 重新开始游戏(音乐重新开始)
  • 出现上面的错误
  • SKAction.playSoundFile 不再总是有效(有些可以,有些不可以)

我的音频处理代码(又名 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()
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在初始化 & 暂停 & 开始通过 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()
    }
}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

Seb*_*ger 5

我不得不将 移动setActive(false)到委托方法。新的助手类如下所示:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在工作