AVAudioSession reactivation issue

戏子无情 提交于 2019-12-06 06:51:03

i had to move the setActive(false0) 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!