【发布时间】:2015-09-28 22:15:30
【问题描述】:
我制作了一个播放音频的简单应用。现在我想在音频播放完毕后自动将“暂停”按钮更改为“播放”。我该如何检查? 代码如下:
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var PausePlay: UIButton!
var BackgroundAudio = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("M.I.A.-DoubleBubbleTrouble", ofType: "mp3")!), error: nil)
override func viewDidLoad() {
super.viewDidLoad()
BackgroundAudio.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func Stop(sender: AnyObject) {
if(BackgroundAudio.playing) {
BackgroundAudio.stop()
BackgroundAudio.currentTime = 0
PausePlay.setTitle("Play", forState: .Normal)
} else {
BackgroundAudio.currentTime = 0
}
}
@IBAction func Restart(sender: AnyObject) {
if(BackgroundAudio.playing==false){
PausePlay.setTitle("Pause", forState: .Normal)
}
BackgroundAudio.stop()
BackgroundAudio.currentTime = 0
BackgroundAudio.play()
}
@IBAction func PausePlay(sender: AnyObject) {
if(BackgroundAudio.playing){
BackgroundAudio.stop()
PausePlay.setTitle("Play", forState: UIControlState.Normal)
} else {
BackgroundAudio.play()
PausePlay.setTitle("Pause", forState: .Normal)
}
}
if (BackgroundAudio.playing == false) {
PausePlay.setTitle("Play", forState: .Normal)
}
}
最后一个 if 语句应该在函数内部,但是我如何(连续)调用该函数?
【问题讨论】: