AVPlayer暂停后不会继续播放

时间:2016-10-17 03:14:46

标签: ios swift avplayer avaudiosession nsnotifications

我刚刚从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 个答案:

答案 0 :(得分:1)

尝试仅在viewWillAppear()MarkupExtension中添加观察者。添加观察者只应在一次性内完成此类问题。 你有任何问题随时可以问我。