您好我是一名初学程序员,正在尝试创建一个基本的音板模板。我的代码在AppDelegate中获得了信号SiGABRT,我不明白为什么。有人可以告诉我我的代码有什么问题,或者提供我可以用来创建音板的代码。这是我到目前为止我的ViewController,任何帮助将不胜感激:
Class SoundListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var audioPlayer = AVAudioPlayer()
var soundsArray: [Sound] = []
var session = AVAudioSession.sharedInstance()
var audioNSURL = NSURL()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
let samplePath = NSBundle.mainBundle().pathForResource("RandomSound", ofType: "m4a")
audioNSURL = NSURL.fileURLWithPath(samplePath!)!
audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL, error: nil)
audioPlayer.prepareToPlay()
var soundPath = NSBundle.mainBundle().pathForResource("RandomSound1", ofType: "m4a")
var soundURL = NSURL.fileURLWithPath(soundPath!)
var sounds1 = Sound()
sounds1.name = "Sound1"
sounds1.URL = soundURL!
self.soundsArray.append(sounds1)
soundPath = NSBundle.mainBundle().pathForResource("RandomSound2", ofType: "m4a")
soundURL = NSURL.fileURLWithPath(soundPath!)
var sounds2 = Sound()
sounds2.name = "Sound2"
sounds2.URL = soundURL!
self.soundsArray.append(sounds2)
soundPath = NSBundle.mainBundle().pathForResource("RandomSound3", ofType: "m4a")
soundURL = NSURL.fileURLWithPath(soundPath!)
var sounds3 = Sound()
sounds3.name = "Sound3"
sounds3.URL = soundURL!
self.soundsArray.append(sounds3)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.soundsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.textLabel?.text = self.soundsArray[indexPath.row].name
println(cell)
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var sound = self.soundsArray[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
var pathComponents = [baseString, sound.URL]
var rowSoundURL = NSURL.fileURLWithPathComponents(pathComponents)!
if audioPlayer.playing && rowSoundURL == audioNSURL {
audioPlayer.stop()
} else {
audioNSURL = rowSoundURL
self.audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL, error: nil)
self.audioPlayer.play()
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
答案 0 :(得分:0)
它在您的代码中是一个简单的错误;-)在viewDidLoad() - 方法中,您只需将soundURL变量设置为媒体文件的完整路径(在本例中为RandomSound1.m4a等)< / p>
在didSelectRowAtIndexPath() - 表视图委托的方法中,您执行以下操作:
var sound = self.soundsArray[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
var pathComponents = [baseString, sound.URL]
然后
var rowSoundURL = NSURL.fileURLWithPathComponents(pathComponents)!
这就是应用程序崩溃的部分。发生这种情况是因为NSURL类尝试根据baseString和sound.URL创建URL(如RFC 1808,1738和2732中定义的)。如果将这些检查到变量,它将如下所示:
sound.URL => "file:///private/var/mobile/Containers/Bundle/Application/2D30CB30-919A-4253-AEFD-7386ED63A553/soundboard.app/take_5.m4a"
baseString => "/var/mobile/Containers/Data/Application/63ADE8EB-DAE0-49E0-83A7-88378F675F9D/Documents"
因此,当您现在组合这两个变量时,路径格式错误,导致崩溃。相反,你可以使用sound.URL,它会做你想要的。
我是这样实现的:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var sound = self.soundsArray[indexPath.row]
var baseString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
if let rowSoundURL = sound.URL {
if audioPlayer.playing && rowSoundURL == audioNSURL {
audioPlayer.stop()
} else {
audioNSURL = rowSoundURL
self.audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL, error: nil)
self.audioPlayer.play()
}
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
注意:在我的例子中,Sound的URL变量是可选的。让我知道它是否有效