尝试在 Swift 中播放声音时出现致命错误
Getting a fatal error when trying to play sound in Swift
我已经完成了本教程 here。您要做的最后一件事是让按钮在按下时发出声音。然后我想继续使用相同的逻辑来制作音板应用程序。但是,当我剥离除了在新项目中发出噪音之外的非必要部分时,我开始遇到致命错误。
这是我的 ViewController.swfit 文件:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var sample : AVAudioPlayer?
func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? {
//1
let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
let url = NSURL.fileURLWithPath(path!)
//2
var audioPlayer:AVAudioPlayer?
// 3
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
} catch {
print("Player not available")
}
return audioPlayer
}
override func viewDidLoad() {
super.viewDidLoad()
if let sample = self.setupAudioPlayerWithFile("Stomach", type:"aif") {
self.sample = sample
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed() {
sample?.play()
}
}
这是我遇到致命错误时的项目
我也试过这个但是我也有错误。
我 运行 在 El Capitan 10.11.3 上 Xcode 7.3
您是否在项目中包含了名为 "Stomach.aif" 的音频文件?否则,pathForResource
将 return 为 nil,当您试图强制展开该路径时,您会崩溃。您可以使用此函数的更安全版本,但如果它找不到该文件,它仍然不会播放音频从好的方面来说,它不应该崩溃。
func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? {
var audioPlayer:AVAudioPlayer? = nil
if let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String) {
let url = NSURL.fileURLWithPath(path)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
} catch {
print("Player not available")
}
}
return audioPlayer
}
我已经完成了本教程 here。您要做的最后一件事是让按钮在按下时发出声音。然后我想继续使用相同的逻辑来制作音板应用程序。但是,当我剥离除了在新项目中发出噪音之外的非必要部分时,我开始遇到致命错误。
这是我的 ViewController.swfit 文件:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var sample : AVAudioPlayer?
func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? {
//1
let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
let url = NSURL.fileURLWithPath(path!)
//2
var audioPlayer:AVAudioPlayer?
// 3
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
} catch {
print("Player not available")
}
return audioPlayer
}
override func viewDidLoad() {
super.viewDidLoad()
if let sample = self.setupAudioPlayerWithFile("Stomach", type:"aif") {
self.sample = sample
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed() {
sample?.play()
}
}
这是我遇到致命错误时的项目
我也试过这个
我 运行 在 El Capitan 10.11.3 上 Xcode 7.3
您是否在项目中包含了名为 "Stomach.aif" 的音频文件?否则,pathForResource
将 return 为 nil,当您试图强制展开该路径时,您会崩溃。您可以使用此函数的更安全版本,但如果它找不到该文件,它仍然不会播放音频从好的方面来说,它不应该崩溃。
func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? {
var audioPlayer:AVAudioPlayer? = nil
if let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String) {
let url = NSURL.fileURLWithPath(path)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
} catch {
print("Player not available")
}
}
return audioPlayer
}