Swift - 音频 - 不断收到按钮声音错误
Swift - Audio - keep getting error for button sound
我刚刚开始在 iOS 中开发并尝试创建简单的应用程序,当我按下按钮时一次又一次地改变图像和播放声音,但在运行它时不断出现错误。
错误“致命错误:在展开可选值时意外发现 nil
(lldb)"
这是我的 "view controller.swift"
import UIKit
import AVFoundation
class ViewController: UIViewController {
let image0 = UIImage(named: "button.png")
let image1 = UIImage(named: "buttonPressed.png")
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "m4a")!)
var player = AVAudioPlayer()
@IBOutlet weak var clicker: UIButton!
@IBAction func clickerPressed(sender: UIButton) {
sender.setImage(image1, forState:.Highlighted)
player.stop()
player.currentTime = 0
player.play()
}
override func viewDidLoad() {
super.viewDidLoad()
clicker.setImage(image0, forState:.Normal)
clicker.setImage(image1, forState:.Highlighted)
player = AVAudioPlayer(contentsOfURL: sound, error: nil)
player.prepareToPlay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
谢谢你,迈克尔。问题已解决。
显然原始 .m4a 文件有问题。我将它转换为 .mp3 并且一切正常。
我刚刚开始在 iOS 中开发并尝试创建简单的应用程序,当我按下按钮时一次又一次地改变图像和播放声音,但在运行它时不断出现错误。
错误“致命错误:在展开可选值时意外发现 nil (lldb)"
这是我的 "view controller.swift"
import UIKit
import AVFoundation
class ViewController: UIViewController {
let image0 = UIImage(named: "button.png")
let image1 = UIImage(named: "buttonPressed.png")
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "m4a")!)
var player = AVAudioPlayer()
@IBOutlet weak var clicker: UIButton!
@IBAction func clickerPressed(sender: UIButton) {
sender.setImage(image1, forState:.Highlighted)
player.stop()
player.currentTime = 0
player.play()
}
override func viewDidLoad() {
super.viewDidLoad()
clicker.setImage(image0, forState:.Normal)
clicker.setImage(image1, forState:.Highlighted)
player = AVAudioPlayer(contentsOfURL: sound, error: nil)
player.prepareToPlay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
谢谢你,迈克尔。问题已解决。 显然原始 .m4a 文件有问题。我将它转换为 .mp3 并且一切正常。