Swift - 选择不同的声音时不播放不同的声音

时间:2015-08-23 01:36:46

标签: ios swift audio

在Swift中,当在TableView中选择不同的声音时,声音的名称将添加到变量中。在不同的视图控制器中,当按下按钮时,它应该访问变量并播放声音,尽管它只是播放相同的声音。这是代码:

FirstSoundController(播放声音):

class FirstViewController: UIViewController {
    var someVariable = SecondViewController()
    @IBAction func activation(sender: UIButton) {
        if sender.titleLabel!.text == "ACTIVATE" {
            sender.setTitle("DEACTIVATE", forState: UIControlState.Normal)

            var someSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(someVariable.soundSelected, ofType: "mp3")!)

            audioPlayer = AVAudioPlayer(contentsOfURL: someSound, error: nil)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        }
        else {
            sender.setTitle("ACTIVATE", forState: UIControlState.Normal)
            audioPlayer.stop()
        }
    }
}

SecondViewController(显示声音表):

class SecondViewController: UIViewController, UITableViewDataSource {
    var sounds = ["BananaSlap", "GlassBreaking", "scream", "WoodyWood", "LaughAndApplause", "EvilLaugh", "Grenade", "BadamTss", "BombExploding"]
    var soundSelected:String?
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sounds.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
        cell.textLabel?.text = sounds[indexPath.row]
        return cell
    }
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        var soundSelected = sounds[indexPath.row]
        println(soundSelected)
    }

}

3 个答案:

答案 0 :(得分:0)

我可以立即发现您SecondViewController中的错误,因为您已将soundSelected重新声明为函数中的局部变量,忽略该属性。所以,而不是:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    var soundSelected = sounds[indexPath.row]
    println(soundSelected)
}

我会尝试:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    soundSelected = sounds[indexPath.row]
    println(soundSelected)
}

答案 1 :(得分:0)

只需改变基本的东西。试试这个:

  1. soundName中声明名为AppDelegate的媒体资源。

  2. 选择要播放的声音时,会使用所选的声音名称初始化soundName

  3. FirstViewControllersoundName获取AppDelegate并播放声音。

  4. 您可以在整个应用中使用AppDelegate进行这些类型的基本数据共享。还有很多其他方法可以执行此操作,例如您可以将声音名称存储在NSUserDefault中并从那里访问它。

    希望这会有所帮助。 :)

答案 2 :(得分:0)

看起来audioPlayer仅在调用someVariable.soundSelected时被重新分配(activation:}。因此,当在表视图中选择一行时 - 它只是记录这一行,而不会启动任何可能改变audioPlayer当前正在播放的操作。所以你描述的行为实际上是预期的。为了实现你所追求的目标,我想到了很多解决方案,即:

  • 使用delegationFirstViewController知道何时选择了一个单元格,以便它可以重新分配audioPlayer
  • 使用notifications(如果您希望将它们合理分开),以便FirstViewController观察点击单元格时SecondViewController发布的通知(它不需要传递值) ,只需触发重新加载audioPlayer
  • audioPlayer移动到全局范围,以及将声音名称作为参数的全局函数,只需使用它重新加载audioPlayer。这样,两个类都不必相互通信,并且可以分别设置音频。这对我来说更有意义(或者至少有一个单独的类来处理音频,因为它更像是一个“单一的”实体)