0
votes

I just switched from AVAudioPlayer to AVPlayer and I'm going through my old functions and making the appropriate adjustments. The AVPlayer is being used to play remote audio files using URL's. When I select a file to play and pause everything works perfectly. However, when I want to resume playing the paused file, the player won't play even though I see the play function is being called. I set up an observer to know when the player is done playing so the play/pause button can toggle and what I noticed is that the observer is getting called after I hit pause. This shouldn't happen since the player isn't done playing right? Anyways, I set breakpoints and everything is getting called correctly. Any idea why the AVPlayer won't resume playing after being paused?

var playerItem: AVPlayerItem?
var newPlayer: AVPlayer?
var trackIDplaying: Int?

func playPausePressed(_ sender:UIButton) 
{

    if let selectedTrackID = trackIDplaying
    {

         do 
         {

            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)

            if selectedTrackID == track.trackId
            {
                //if playing, then pause.  If not playing, then play
                if (self.newPlayer!.rate != 0)
                {
                    self.newPlayer!.pause()
                    self.isPaused = true   
                }
                else
                {
                    if self.newPlayer!.currentItem != nil
                    {
                        self.newPlayer!.play()
                        print(self.newPlayer!.currentItem)
                    }
                }
            }
            else
            {
                //If song is playing, switch to new song
                let trackURL = URL(string: track.preSignedURL!)
                trackIDplaying = track.trackId
                self.playerItem = AVPlayerItem(url: trackURL!)
                NotificationCenter.default.addObserver(self, selector: #selector((HomeController.playerDidFinishPlaying)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerItem)
                self.newPlayer = AVPlayer(playerItem: self.playerItem!)
                self.newPlayer!.play()
            }
         }
         catch let error1 as NSError 
         {
            error = error1
            self.newPlayer = nil
         }
    }
    else 
    {
         do 
         {
             //play selected song if no other songs are playing
            let trackURL = URL(string: track.preSignedURL!)
            print(trackURL!)
            self.playerItem = AVPlayerItem(url: trackURL!)
            NotificationCenter.default.addObserver(self, selector: #selector((HomeController.playerDidFinishPlaying)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerItem)

            self.newPlayer = AVPlayer(playerItem: self.playerItem!)
            self.newPlayer!.play()
            self.trackIDplaying = track.trackId
         }
    }
    if let err = error 
    {
         print("audio player error \(err.localizedDescription)", terminator: "")
    }
}

func playerDidFinishPlaying(note: NSNotification) {

    guard let indexPath = self.playingAudioIndexPath, let cell = self.audioTable.cellForRow(at: indexPath) as? AudioCell else {

        return;
    }

    cell.playButton.isSelected = false
    self.playingAudioIndexPath = nil

}
1

1 Answers

1
votes

try adding observer in only viewDidLoad() or viewWillAppear() adding observer should only done in one time for these kind of problems. Still you have any problem feel free to ask me.