Swift 2.2 单播放/暂停 UIButton

标签 swift function avaudioplayer

我正在尝试编写一个函数,其中: 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/

相关文章:

python - 是否可以不从 python 中的函数返回任何内容?

iphone - 如何在 AVAudioPlayer 中静音?

ios - 如何从UISlider 的自定义位置播放音频?

iphone - audioPlayerDidFinishPlaying : called之后的EXC_BAD_ACCESS

arrays - 无法为数组 Swift 随机选择一个元素

ios - 更改自定义颜色的边框颜色

ios - Swift 数组大小限制和 Xcode 编译限制

php - 如何在函数中使用外部变量?

Python:如何正确使用外部函数

ios - 如何判断图片中是否没有文字--MLKit VisionCloudTextRecognizer