可重复使用的扩展,在iOS和Swift中播放声音而不会出现内存泄漏



播放声音在代码中非常冗长,如果可能的话,我们希望创建AVAudioSession的扩展。我这样做的方式是将一个对象分配给一个变量,但需要关于如何设置这个函数的帮助/建议,以便它可以重复使用和优化。

这是我的:

func playSound(name: String, extension: String = "mp3") {
    let sound = NSBundle.mainBundle().URLForResource(name, withExtension: extension)
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
        audioPlayer = try AVAudioPlayer(contentsOfURL: sound!)
        audioPlayer?.prepareToPlay()
        audioPlayer?.play()
    } catch { }
}

我想我必须在这个函数之外创建audioPlayer变量,否则我很难玩任何东西。也许它可以自我控制?我希望像这样使用它:

AVAudioSession.sharedInstance().play("bebop")

直接从您的示例中获取代码,我看到两个选项:

1) 你想把它作为AVAudioSession的扩展,有什么特别的原因吗?如果没有,就自己服务吧!

class AudioPlayerService {
    static let sharedInstance = AudioPlayerService()
    var audioPlayer: AVAudioPlayer?
    func playSound(name: String, extension: String = "mp3") {
        let sound = NSBundle.mainBundle().URLForResource(name, withExtension: extension)
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)
            UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
            audioPlayer = try AVAudioPlayer(contentsOfURL: sound!)
            audioPlayer?.prepareToPlay()
            audioPlayer?.play()
        } catch { }
    }
}

2) 如果确实需要将其作为AVAudioSession的扩展,请查看相关对象

extension AVAudioSession {
    private struct AssociatedKeys {
        static var AudioPlayerTag = "AudioPlayerTag"
    }
    var audioPlayer: AVAudioPlayer? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.AudioPlayerTag) as? AVAudioPlayer
        }
        set {
            if let newValue = newValue {
                objc_setAssociatedObject(
                    self,
                    &AssociatedKeys.AudioPlayerTag,
                    newValue as AVAudioPlayer?,
                    .OBJC_ASSOCIATION_RETAIN_NONATOMIC
                )
            }
        }
    }
    func playSound(name: String, extension: String = "mp3") {
        let sound = NSBundle.mainBundle().URLForResource(name, withExtension: extension)
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)
            UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
            audioPlayer = try AVAudioPlayer(contentsOfURL: sound!)
            audioPlayer?.prepareToPlay()
            audioPlayer?.play()
        } catch { }
    }
}

这是我发现的添加声音的最佳方法

文件名为shootMissile.wav

func shootMissileSound() {
    if let soundURL = NSBundle.mainBundle().URLForResource("shootMissile", withExtension: "wav") {
        var mySound: SystemSoundID = 0
        AudioServicesCreateSystemSoundID(soundURL, &mySound)
        AudioServicesPlaySystemSound(mySound);
    }
}

最新更新