如何在Swift 2的iOS应用程序中使用音频?

10 avfoundation swift swift2

我使用SpriteKit和Swift在Xcode 7 beta上制作了一个游戏,我试图将音频放入其中,但它不可能,因为Xcode发现3个错误,我是初学者,我不知道如何修复它们.

import AVFoundation

    var audioPlayer = AVAudioPlayer()


    func playAudio() {
        // Set the sound file name & extension
        var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav")!)

        // Preperation
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
        AVAudioSession.sharedInstance().setActive(true, error: nil)

        // Play the sound
        var error: NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
}
Run Code Online (Sandbox Code Playgroud)

代码错误在这里:

代码1:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
Run Code Online (Sandbox Code Playgroud)

错误1:

调用中的额外参数'错误'

代码2:

AVAudioSession.sharedInstance().setActive(true, error: nil)
Run Code Online (Sandbox Code Playgroud)

错误2:

调用中的额外参数'错误'

代码3:

audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
Run Code Online (Sandbox Code Playgroud)

错误3:

找不到类型'AVAudioPlayer'的初始值设定项,它接受类型'的参数列表'(contentsOfURL:NSURL,错误:inout NSError?)'

您的贡献可能会对我有所帮助.谢谢.

aya*_*aio 24

使用里面的功能do catchtry用于投掷的功能:

var audioPlayer = AVAudioPlayer()

func playAudio() {
    do {
        if let bundle = NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav") {
            let alertSound = NSURL(fileURLWithPath: bundle)
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)
            try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        }
    } catch {
        print(error)
    }
}
Run Code Online (Sandbox Code Playgroud)

有关完整的Swift 2错误处理说明,请参阅此答案.


Sai*_*ira 6

Swift 2.0中的处理错误已经改变.基础和其他系统框架现在使用新类型的错误处理,使用try和catch机制.

在Cocoa中,产生错误的方法采用NSError指针参数last参数,如果发生错误,该参数会使用NSError对象填充其参数.Swift会自动将产生错误的Objective-C方法转换为根据Swift的本机错误处理功能引发错误的方法.

如果你查看你使用的函数的定义,你可以看到它们现在抛出.例如:

public func setActive(active: Bool) throws
Run Code Online (Sandbox Code Playgroud)

您可以看到没有错误参数,因为函数抛出错误.这大大减少了NSError处理的痛苦,并且还减少了您需要编写的代码量.

因此,无论您将错误视为函数中的最后一个参数,请将其删除并编写try!在它面前.一个例子是:

try! AVAudioSession.sharedInstance().setActive(true)
Run Code Online (Sandbox Code Playgroud)

由于错误处理不是此问题的范围,您可以在此处阅读更多相关信息.

这是您编写的代码的更正版本:

import AVFoundation

var audioPlayer = AVAudioPlayer()

func playAudio() {
    // Set the sound file name & extension
    let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav")!)

    // Preperation
    try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])
    try! AVAudioSession.sharedInstance().setActive(true)

    // Play the sound
    do {
        try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    } catch {
        print("there is \(error)")
    }
}
Run Code Online (Sandbox Code Playgroud)