How can I know if AVAudioPlayer is paused?
Asked Answered
R

4

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.

Rettarettig answered 28/9, 2016 at 10:31 Comment(5)
check for audioplayer.statusCurvaceous
@Curvaceous 'AVAudioPlayer' has no member 'status'Rettarettig
let me post complete code for youCurvaceous
I am not sure about your issue, but i guess you should make the currentTime set to zero when you are stopping the audio. then can check currentTime. if zero then stop else pause. Does it make sense??Domeniga
You make audioPlayer = nil when you finish playing.Mehta
T
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.

Thearchy answered 28/9, 2016 at 10:38 Comment(0)
C
0

Its made from this this link:

 if ((player.rate != 0) && (player.error == nil)) {
        // player is playing
    }else {
//notplaying
}
Curvaceous answered 28/9, 2016 at 10:39 Comment(2)
still you need any help feel free to ask meCurvaceous
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!Mehta
E
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)
    }
}
Egoist answered 2/3, 2019 at 6:57 Comment(0)
C
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()
    }
}
Chancemedley answered 25/1, 2023 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.