我正在尝试根据用户选择的乐器播放歌曲。这是我的代码:
@IBAction func play(sender:AnyObject){
if isPlaying == false {
player.play()
isPlaying = true
}
}
@IBAction func stop(sender: AnyObject) {
if isPlaying == true {
player.stop()
isPlaying = false
}
}
@IBAction func pause(sender: AnyObject) {
if isPlaying == true {
isPlaying = false
player.pause()
}
}
var player = AVAudioPlayer()
var audioPath = NSBundle.mainBundle().pathForResource("Iditarod Bass", ofType: "m4a")
var isPlaying = Bool()
@IBOutlet var instrumentSelect: UISegmentedControl!
@IBAction func didChangeInstrument(sender: AnyObject) {
if isPlaying == false {
if instrumentSelect.selectedSegmentIndex == 0 {
audioPath = NSBundle.mainBundle().pathForResource("Iditarod Bass", ofType: "m4a")
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
} catch {
}
} else if instrumentSelect.selectedSegmentIndex == 1 {
audioPath = NSBundle.mainBundle().pathForResource("Iditarod Cello", ofType: "m4a")
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
} catch {
}
} else if instrumentSelect.selectedSegmentIndex == 2 {
audioPath = NSBundle.mainBundle().pathForResource("Iditarod Viola", ofType: "m4a")
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
} catch {
}
} else if instrumentSelect.selectedSegmentIndex == 3 {
audioPath = NSBundle.mainBundle().pathForResource("Iditarod Violin 1", ofType: "m4a")
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
} catch {
}
} else if instrumentSelect.selectedSegmentIndex == 4 {
audioPath = NSBundle.mainBundle().pathForResource("Iditarod Violin 2", ofType: "m4a")
do {
try player = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath!))
} catch {
}
}
}
}
除instrumentSelect.selectedSegmentIndex == 1
。以外的其他都工作正常。我不知道为什么 我得到一个EXC_BAD_INSTRUCTION
说fatal error: unexpectedly found nil while unwrapping an Optional value
。该文件运行正常,我已经多次清理了该项目。我正确键入了所有内容。为什么那一个不起作用?我对此感到非常困惑,也感谢您的帮助。
该文件未添加到应用程序目标中。它仅被添加到项目中。感谢@matt为我提供帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句