Swift音板应用程序

Swift音板应用程序,swift,tableview,viewcontroller,appdelegate,Swift,Tableview,Viewcontroller,Appdelegate,大家好,我是一个初学者程序员,正在使用swift创建一个基本的音板模板。我的代码在AppDelegate中收到信号SiGABRT,我不知道为什么。有人能告诉我我的代码有什么问题,或者提供我可以用来创建音板的代码吗。这是我迄今为止为我的ViewController所做的一切,如有任何帮助,将不胜感激: Class SoundListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

大家好,我是一个初学者程序员,正在使用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)

}

这是代码中的一个简单错误;-)在viewDidLoad()-方法中,您只需将soundURL变量设置为媒体文件的完整路径(在这种情况下,设置为RandomSound1.m4a,依此类推)

在表视图委托的didSelectRowatineXpath()方法中,执行以下操作:

var sound = self.soundsArray[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
var pathComponents = [baseString, sound.URL]
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" 
然后

var rowSoundURL = NSURL.fileURLWithPathComponents(pathComponents)!
这就是应用程序崩溃的原因。这是因为NSURL类试图根据baseString和sound.URL创建URL(如RFCs 1808、1738和2732中定义的URL)。如果将这些变量选中,它将如下所示:

var sound = self.soundsArray[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
var pathComponents = [baseString, sound.URL]
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变量是可选的。让我知道它是否有效

这是代码中的一个简单错误;-)在viewDidLoad()-方法中,您只需将soundURL变量设置为媒体文件的完整路径(在这种情况下,设置为RandomSound1.m4a,依此类推)

在表视图委托的didSelectRowatineXpath()方法中,执行以下操作:

var sound = self.soundsArray[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
var pathComponents = [baseString, sound.URL]
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" 
然后

var rowSoundURL = NSURL.fileURLWithPathComponents(pathComponents)!
这就是应用程序崩溃的原因。这是因为NSURL类试图根据baseString和sound.URL创建URL(如RFCs 1808、1738和2732中定义的URL)。如果将这些变量选中,它将如下所示:

var sound = self.soundsArray[indexPath.row]
var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
var pathComponents = [baseString, sound.URL]
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变量是可选的。让我知道它是否有效

谢谢你的帮助,我对编码是新手,我非常希望能有一个应用程序。当我实现您的代码时,出现了一个错误,说“条件绑定中的绑定值必须是最佳类型。”当我取出第一个if语句时,它只有:let rowSoundURL=sound.URL{出现了一个错误,说“无法使用类型为(0->)的参数列表调用URL”我不太确定从这里做什么。欢迎你!哪一行出现错误?我使用XCluts6.3.2,我认为它使用SWIFT 1.2。实际上,我的声音对象看起来是这样的:“导入基础类声音:NStase{var名称:Stras.var URL:nSURL}?'如果您仍然有问题,只需向我展示您的声音对象的外观,这样我就可以更新我的代码。我也有相同的Sound.swift对象。Xcode在let rowSoundURL=Sound.URL上给我一个错误{line表示Sound.type没有名为URL的成员,尽管我很确定它有。以及if audioPlayer.playing&&rowSoundURL==audioNSURL{and audioNSURL=rowSoundURL在其初始值中使用了错误变量。我猜这是因为rowSoundURL工作不正常,但我不是100%确定。感谢您的帮助,我对编码还不熟悉,我非常希望有一个应用程序。当我实现您的代码时,出现了一个错误,说“条件绑定中的绑定值必须是最佳类型。”当我取出第一个if语句,使其具有:let rowSoundURL=sound.URL{时,出现了一个错误,表示“无法使用类型为(0->\的参数列表调用URL”。“我不太确定从这里做什么。欢迎你!哪一行出现错误?我使用XCluts6.3.2,我认为它使用SWIFT 1.2。实际上,我的声音对象看起来是这样的:‘导入基础类声音:NStase{var名称:Stras.var URL:nSURL}'如果您仍然有问题,只需向我展示您的声音对象的外观,这样我就可以更新我的代码。我也有相同的Sound.swift对象。Xcode在let rowSoundURL=Sound.URL上给我一个错误{line表示Sound.type没有名为URL的成员,尽管我很确定它有。以及if audioPlayer.playing&&rowSoundURL==audioNSURL{and audioNSURL=rowSoundURL在其初始值中使用了错误变量。我猜这是因为rowSoundURL工作不正常,但我不是100%确定。感谢您的帮助,我对编码还不熟悉,我非常希望有一个应用程序。当我实现您的代码时,出现了一个错误,说“条件绑定中的绑定值必须是最佳类型。”当我取出第一个if语句,使其具有:let rowSoundURL=sound.URL{时,出现了一个错误,表示“无法使用类型为(0->\的参数列表调用URL”。“我不太确定从这里做什么。欢迎你!哪一行出现错误?我使用XCluts6.3.2,我认为它使用SWIFT 1.2。实际上,我的声音对象看起来是这样的:‘导入基础类声音:NStase{var名称:Stras.var URL:nSURL}'如果您仍然有问题,只需向我展示您的声音对象的外观,这样我就可以更新我的代码。我也有相同的Sound.swift对象。Xcode在let rowSoundURL=Sound.URL上给我一个错误{line表示Sound.type没有名为URL的成员,尽管我很确定它有。以及if audioPlayer.playing&&rowSoundURL==audioNSURL{and audioNSURL=rowSoundURL在其初始值中使用了错误变量。我猜这是因为rowSoundURL工作不正常,但我不能100%确定。