Search code examples
swiftfunctionavaudioplayer

Swift 2.2 single play/pause UIButton


I am trying to write a function where: 1. If AVAudioPlayer is not playing, it sequentially plays an array of NSURLs. 2. If playing, the same button pauses playback.

My code successfully does 1 but not 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()
        }
}

Not too sure where I'm going wrong -- any advice would be appreciated.


Solution

  • You need to change your button callback to the following:

    @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()
        }
    }
    

    In particular, note the else if instead of if. This way, the button doesn't start playing if it's already playing.