gpt4 book ai didi

Swift 2.2 单播放/暂停 UIButton

转载 作者:行者123 更新时间:2023-11-30 13:23:55 24 4
gpt4 key购买 nike

我正在尝试编写一个函数,其中:1. 如果 AVAudioPlayer 没有播放,它会顺序播放 NSURL 数组。2. 如果正在播放,同一按钮可暂停播放。

我的代码成功执行了 1 但未执行 2:

lazy var isPlaying = Bool()
lazy var isPaused = Bool()
//...
@IBAction func e1Play() {
self.e1delegate = nil

//Start
if ePlaylist.count > 0 && eCurrentTrack < ePlaylist.count{
do{
try ePlayer = AVAudioPlayer(contentsOfURL: ePlaylist[eCurrentTrack])
} catch {
print("ePlayer not available")
}
ePlayer?.prepareToPlay()
refreshlabel()
isPlaying = true
isPaused = false
e1PlayBtn!.setImage(UIImage(named: "pause-icon.png"), forState: UIControlState.Normal)
ePlayer!.delegate = self
ePlayer!.play()
}

//Pause
if isPlaying == true && isPaused == false {
ePlayer!.pause()
isPlaying = false
isPaused = true
e1PlayBtn!.setImage(UIImage(named: "play-icon.png"), forState: UIControlState.Normal)
isPlaying = false
}
//Resume
if isPlaying == false && isPaused == true {
ePlayer?.prepareToPlay()
ePlayer!.delegate = self
e1PlayBtn!.setImage(UIImage(named: "pause-icon.png"), forState: UIControlState.Normal)
isPlaying = true
isPaused = false
ePlayer!.play()
}
}

func refreshlabel() {
let index = eTestarray![eCurrentTrack]
let description = eDescriptions[index]
currentlyplaying!.text = "Currently Playing: \n\(description)"
}

func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool){
self.e1delegate?.soundFinished(self)
let totaltracks:Int = ePlaylist.count - 1
if eCurrentTrack == totaltracks{
eCurrentTrack = 0
currentlyplaying!.text = "Currently Playing: "
e1PlayBtn!.setImage(UIImage(named:"play-icon.png"), forState: UIControlState.Normal)
}
else{

eCurrentTrack += 1
//If needed, add time delay here.
e1Play()
}
}


@IBAction func e1Next() {
if eCurrentTrack+1 > ePlaylist.count {
eCurrentTrack = 0
refreshlabel()
} else {
eCurrentTrack+1;
refreshlabel()
}
}

@IBAction func e1Previous(){
if eCurrentTrack-1 < 0 {
eCurrentTrack = (ePlaylist.count - 1) < 0 ? 0 : (ePlaylist.count - 1)
refreshlabel()
} else {
eCurrentTrack-1
refreshlabel()
}
}

不太确定我哪里出错了——任何建议将不胜感激。

最佳答案

您需要将按钮回调更改为以下内容:

@IBAction func e1Play() { 
self.e1delegate = nil

//Start
if isPlaying == false && isPaused == false {
if ePlaylist.count > 0 && eCurrentTrack < ePlaylist.count{
do{
try ePlayer = AVAudioPlayer(contentsOfURL: ePlaylist[eCurrentTrack])
} catch {
print("ePlayer not available")
}
ePlayer?.prepareToPlay()
refreshlabel()
isPlaying = true
isPaused = false
e1PlayBtn!.setImage(UIImage(named: "pause-icon.png"), forState: UIControlState.Normal)
ePlayer!.delegate = self
ePlayer!.play()
}
}

//Pause
else if isPlaying == true && isPaused == false {
ePlayer!.pause()
isPlaying = false
isPaused = true
e1PlayBtn!.setImage(UIImage(named: "play-icon.png"), forState: UIControlState.Normal)
isPlaying = false
}
//Resume
else if isPlaying == false && isPaused == true {
ePlayer?.prepareToPlay()
ePlayer!.delegate = self
e1PlayBtn!.setImage(UIImage(named: "pause-icon.png"), forState: UIControlState.Normal)
isPlaying = true
isPaused = false
ePlayer!.play()
}
}

特别要注意 else if 而不是 if。这样,如果按钮已经在播放,则不会开始播放。

关于Swift 2.2 单播放/暂停 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com