6

To discover whether an AVAudioPlayer object is playing, I can simply use the isPlaying property:

if audioPlayer.isPlaying {
    // ...
}

If the audio player is not playing however, how can I distinguish between it being paused and it being stopped?

I can't use the currentTime property for this purpose. This is because if I call stop(), then the current time stays at whatever time it was at the time of the call, which is the same behaviour as when pause() is called.

Naresh
  • 16,698
  • 6
  • 112
  • 113
Flimm
  • 136,138
  • 45
  • 251
  • 267

4 Answers4

0

I think it's as simple as keep a BOOL property in view controller class which set to YES while play/resume buttons clicked and set to NO while stop/pause button pressed.

Anand Prem
  • 397
  • 5
  • 15
0

Its made from this this link:

 if ((player.rate != 0) && (player.error == nil)) {
        // player is playing
    }else {
//notplaying
}
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
JAck
  • 854
  • 9
  • 18
  • still you need any help feel free to ask me – JAck Sep 28 '16 at 10:39
  • Jack, don't want to vote this down, but this doesn't work if you pause and then look again to see if its playing. The logical test audioPlayer.isPlaying does, we're talking iOS 10, swift 3 here. The rate once it starts playing doesn't seem to be set to zero. Maybe a bug, maybe you need to use isPlaying! – user3069232 Jun 18 '17 at 18:50
0

Simply add Bool variable and based on that you check is audio paused or not.

Ex:

var isAudioPaused = false//Add this on top

//Pause audio
func pauseAudio() {
    if isAudioPaused == false {
        if audioPlayer?.isPlaying == true {//Check is audio playing
            audioPlayer?.pause()//Pause audio
            isAudioPaused = true
            btnPauseAudio.setTitle("Play Audio", for: .normal)
        }
    } else {
        audioPlayer?.play()//Play audio
        isAudioPaused = false
        btnPauseAudio.setTitle("Pause Audio", for: .normal)
    }
}
Naresh
  • 16,698
  • 6
  • 112
  • 113
0

You can check player that is paused or not though code that is player.isPlaying && player.duration != 0

func buttonOneClicked(button: UIButton)  {
    if player.isPlaying && player.duration != 0 {
       button.setImage(UIImage(systemName: "stop"), for: .normal)
       player.pause()
    }
    else{
       button.setImage(UIImage(systemName: "play"), for: .normal)
       player.play()
    }
}