IBAction按钮:我想点击或单击一次停止并单击或再次点击再次播放

时间:2016-10-22 20:01:28

标签: swift swift3 ibaction

@IBAction func stopMusic(_ sender: AnyObject) {
    if player != nil {
        player.stop()
    } 
    else{ 
        player.play()
    }
}

1 个答案:

答案 0 :(得分:0)

var player: AVAudioPlayer?
@IBAction func stopMusic(_ sender: AnyObject) {
    if var p = player {
        if p.playing {
            p.stop()
        }
        else {
            p = AVAudioPlayer()
            p.prepareToPlay()
            p.play()
        }
    }
}


override func viewDidLoad() {
    player = AVAudioPlayer()
}