首先让我说我对编程很新.我想要做的是添加一个按钮,当按下播放音乐时,再次按下音乐停止.理想情况下,第三次按下按钮时,音乐将重置.在尝试实现这一点时,我收到错误消息“表达式解析为未使用的功能”,因为我非常新的所有的帮助,我在网上找到没有任何意义.
原文链接:https://www.f2er.com/swift/319636.htmlimport UIKit import AVFoundation class ViewController: UIViewController { @IBOutlet weak var janitor: UIImageView! var pianoSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C",ofType: "m4a")!) var audioPlayer = AVAudioPlayer() override func viewDidLoad() { super.viewDidLoad() audioPlayer = AVAudioPlayer(contentsOfURL: pianoSound,error: nil) audioPlayer.prepareToPlay() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func PianoC(sender: AnyObject) { audioPlayer.play() if audioPlayer.playing { audioPlayer.stop} else { audioPlayer.play} } }