【发布时间】:2015-06-09 21:27:32
【问题描述】:
您好,我是一名使用 swift 的初学者程序员,正在尝试创建一个基本的音板模板。我的代码一直在 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)
}
【问题讨论】:
标签: swift tableview viewcontroller appdelegate