AVPlayer 暂停后无法继续播放

Posted

技术标签:

【中文标题】AVPlayer 暂停后无法继续播放【英文标题】:AVPlayer won't resume playing after being paused 【发布时间】:2016-10-17 03:14:46 【问题描述】:

我刚刚从AVAudioPlayer 切换到AVPlayer,我正在检查我的旧功能并进行适当的调整。 AVPlayer 用于使用 URL 播放远程音频文件。当我选择要播放和暂停的文件时,一切正常。但是,当我想继续播放暂停的文件时,即使我看到正在调用播放函数,播放器也不会播放。我设置了一个观察者来了解播放器何时完成播放,因此播放/暂停按钮可以切换,我注意到观察者在我点击暂停后被调用。这不应该发生,因为玩家没有玩完正确的游戏?无论如何,我设置了断点,一切都被正确调用。知道为什么 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


【问题讨论】:

【参考方案1】:

尝试仅在viewDidLoad()viewWillAppear() 中添加观察者 对于此类问题,添加观察者应该只在一次完成。 还有什么问题可以随时问我。

【讨论】:

通过在我点击播放时声明观察者,为什么在切换暂停/播放时会自动跳转到观察者方法。 它成功了,只是想知道为什么我原来的做法不起作用。 是的。正如我告诉你的...... AddObserver Method() 一旦我们调用它就会开始观察。在另一个函数中。它会覆盖所以,有时它找不到正确的删除。如果答案对您有帮助,请投票...

以上是关于AVPlayer 暂停后无法继续播放的主要内容,如果未能解决你的问题,请参考以下文章

AVPlayer,长时间停顿后恢复 = 故障

无法使用自定义按钮暂停 AVPlayer

iOS AVPlayer:屏幕锁定时暂停视频

AVPlayer 播放、暂停和缓冲问题

从 NavigationController 中移除 ViewController 后 AVPlayer 继续播放

如何分配 AVPlayer 播放/暂停按钮 iOS?