首先让我说我对编程很新.我要做的是添加一个按钮,当按下该按钮播放音乐时,再次按下时按钮音乐停止.理想情况下,当第三次按下按钮时,音乐将重置.在尝试实现这一目标的同时,我收到错误消息"表达式解析为未使用的函数",因为我很新,我在网上找到的所有帮助对我都没有任何意义.
import 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}
}
}
Run Code Online (Sandbox Code Playgroud) swift ×1