如何快速停止音频播放器

时间:2014-11-19 02:47:19

标签: ios swift audio-player

因此,在我的应用程序中,当用户点击播放按钮时播放乐器但是当他们停止播放时应该停止音乐。在我的代码中,当我添加停止按钮的代码时没有播放当我点击播放按钮但是当我删除代码时播放按钮工作但停止按钮没有。我不明白为什么它在我的代码中这样做。

这是代码:

@IBAction func playInstrumentalPressed(sender: AnyObject) {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    let saveFileName = nameSavedFile as String
    if (saveFileName != "") {
        let beatLocation = documentsPath + "/" + saveFileName + ".wav"
        let fileURL = NSURL(fileURLWithPath: beatLocation)

        var error:NSError?

        player = AVAudioPlayer(contentsOfURL: fileURL, error: &error)

        if player.prepareToPlay() && player.play() {
            println("yes")
        } else {
            println("no")
        }

    } else {
        println("Not Available") //add alertview here
    }
}

@IBAction func stopInstrumentalButton(sender: UIButton) {
    if (player.playing) {
        player.stop()
    } else {
        println("Nothing is playing")
    }
      }

1 个答案:

答案 0 :(得分:0)

我想知道你的iBAction出口是否错误连接......也许请仔细检查你的xib / storyboard,了解按钮连接的方法

相关问题