heavyguidence
heavyguidence

Reputation: 373

stopping an AVPlayer in swift

My Audio Streaming App which has a single Play / Pause Button and works fine. However I have little problem while using the player.pause() property. Sometimes the stream does not continue after pressing play Button and especially (less often) it happens when application comes to foreground after being sent to background ( for example user opened another music app, now he wants to resume my app and wants to continue again). User may have to kill and restart the app in order to start the player (audio stream).

In short, Can I use any property such as player.stop() to make a full stop to the steaming link and resume after with player.play()

This is what I tries as well, but same problem,:

func playPause() {
    if  (player.rate == 1.0) {
        player.rate = 0.0
        self.titleLabel.text = "Paused"

    }
    else {
        player.play()
        self.titleLabel.text = "Now Playing Live!"
    }
}

Thanks for any suggestions

Upvotes: 0

Views: 4689

Answers (1)

DARKUNIT22
DARKUNIT22

Reputation: 76

I have tested this and it seems to give no problems at all:

 @IBAction func playPauseAction(sender: UIButton) {
      if audioPlayer.rate > 0.0 {
          audioPlayer.pause()
      } else {
          audioPlayer.play()
      }
 }

Upvotes: 1

Related Questions