AVPlayer 暂停后不会继续播放

AVPlayer won't resume playing after being paused

我刚刚从 AVAudioPlayer 切换到 AVPlayer,我正在检查我的旧功能并进行适当的调整。 AVPlayer 正在使用 URL 播放远程音频文件。当我 select 播放和暂停文件时,一切正常。但是,当我想继续播放暂停的文件时,即使我看到正在调用播放功能,播放器也不会播放。我设置了一个观察者来知道玩家什么时候玩完,所以 play/pause 按钮可以切换,我注意到观察者在我按下暂停后被调用。这不应该发生,因为玩家还没有玩完对吗?不管怎样,我设置了断点,一切都被正确调用了。知道为什么 AVPlayer 在暂停后不会继续播放吗?

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

}

尝试只添加观察者 viewDidLoad()viewWillAppear() 添加观察者应该一次性完成这些问题。 还有什么问题随时问我。