Swift 2.2单播/暂停UIButton

时间:2016-05-25 17:55:42

标签: 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()
        }
}

不太确定我哪里出错了 - 任何建议都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

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

Attempt 1
['a', 'b', 'c']
['d', 'e', 'f']
Attempt 2

请特别注意@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。这样,如果按钮已经播放,则该按钮不会开始播放。