简体   繁体   English

如何快速停止音频播放器

[英]How to stop audio player in swift

So in my app the user plays an instrumental when they hit the play button but when they stop it should stop the music. 因此,在我的应用程序中,用户按下播放按钮时会弹奏乐器,但是当他们停止播放时,它应该会停止播放音乐。 In my code when i add the code for the stop button nothing plays when i hit the play button but when i delete the code the play button works but the stop button doesn't. 在我的代码中,当我添加停止按钮的代码时,当我按下“播放”按钮时没有任何播放,但是当我删除代码时,“播放”按钮起作用了,但停止按钮却没有。 I don't see why its doing that in my code. 我不明白为什么在我的代码中这样做。

Here is code: 这是代码:

@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")
    }
      }

我想知道您的iBAction插座是否连接不正确...也许仔细检查您的xib / storyboard xib以查看按钮的连接方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM