AVAudioPath 不工作 - Swift
AVAudioPath not working - Swift
我正在尝试根据用户选择的乐器播放歌曲。这是我的代码:
@IBAction func play(发件人: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 帮助我。
我正在尝试根据用户选择的乐器播放歌曲。这是我的代码:
@IBAction func play(发件人: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 帮助我。