Swift 2.2 单个 play/pause UIButton

Swift 2.2 single play/pause UIButton

我正在尝试编写一个函数,其中: 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。这样,如果按钮已经在播放,则不会开始播放。