Tôi đã thực hiện trò chơi trên Xcode 7 beta bằng SpriteKit và Swift, tôi đã cố gắng đưa Audio vào nó nhưng nó không thể xác định được vì Xcode tìm thấy 3 lỗi, tôi mới bắt đầu và tôi không biết cách sửa chúng.Cách sử dụng ứng dụng âm thanh trong iOS với Swift 2?
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()
}
Lỗi mã đang ở đây:
Mã 1:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
Lỗi 1:
Extra argument 'error' in call
Mã 2:
AVAudioSession.sharedInstance().setActive(true, error: nil)
Lỗi 2:
Extra argument 'error' in call
Mã 3:
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
Lỗi 3:
Cannot find an initializer for type 'AVAudioPlayer' that accepts an argument list of type '(contentsOfURL: NSURL, error: inout NSError?)'
Đóng góp của bạn có thể giúp tôi. Cảm ơn.
thể trùng lặp của [Swift làm-try-catch cú pháp] (http://stackoverflow.com/questions/30720497/swift-do-try-catch-syntax) – nalply