简体   繁体   中英

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以查看按钮的连接方式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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