4

I would like to create a custom video player using AVPlayer() and AVPlayerLayer() classes. My code working fine for application fresh start but goes wrong and showing blank screen when the application running or idle for more than 15 minutes. When the occurrences this issue all classes of my application having AVPlayerLayer showing blank screen. I don't know why this happens.

AVPlayer() and AVPlayerlayer() instances are initialized as below.

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.


        dispatch_async(dispatch_get_main_queue(), { 

            let playerItem = AVPlayerItem(URL: self.itemUrl)
            self.avPlayer = AVPlayer(playerItem: playerItem)
            NSNotificationCenter.defaultCenter().addObserver(self,
                selector: #selector(VideoPreviewView.restartVideoFromBeginning),
                name: AVPlayerItemDidPlayToEndTimeNotification,
                object: self.avPlayer.currentItem)
            self.avPlayerLayer = AVPlayerLayer(player: self.avPlayer)
            self.view.layer.addSublayer(self.avPlayerLayer)
            self.avPlayerLayer.frame = self.view.bounds
            self.avPlayer.pause()
        })

    }
}

Play function

func playVideo(sender: AnyObject) {

    avPlayer.play()
    if (avPlayer.rate != 0 && avPlayer.error == nil) {
        print("playing")
    }
    slider.hidden=false
    myTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(VideoPreviewView.updateSlider), userInfo: nil, repeats: true)
    slider.addTarget(self, action: #selector(VideoPreviewView.sliderValueDidChange(_: )), forControlEvents: UIControlEvents.ValueChanged)
    slider.minimumValue = 0.0
    slider.continuous = true
    pauseButton.hidden=false
    playButton.hidden=true
    closeViewButton.hidden = false
}

Restart video

func restartVideoFromBeginning() {

    let seconds: Int64 = 0
    let preferredTimeScale: Int32 = 1
    let seekTime: CMTime = CMTimeMake(seconds, preferredTimeScale)
    avPlayer?.seekToTime(seekTime)
    avPlayer?.pause()
    pauseButton.hidden=true
    playButton.hidden = false
    closeViewButton.hidden=false
}

func updateSlider() {
    if (avPlayer.rate != 0 && avPlayer.error == nil) {
        print("playing")
    }
    else{
        print("Not playing.")
    }
    currentTime = Float(CMTimeGetSeconds(avPlayer.currentTime()))
    duration = avPlayer.currentItem!.asset.duration
    totalDuration = Float(CMTimeGetSeconds(duration))
    slider.value = currentTime // Setting slider value as current time
    slider.maximumValue = totalDuration // Setting maximum value as total duration of the video
}

 func sliderValueDidChange(sender: UISlider!) {
    timeInSecond=slider.value
    newtime = CMTimeMakeWithSeconds(Double(timeInSecond), 1)// Setting new time using slider value
    avPlayer.seekToTime(newtime)
}

@IBAction func closeViewAction(sender: AnyObject) {
    pauseButton.hidden=true
    self.avPlayer.pause()

    self.avPlayerLayer.removeFromSuperlayer()
    self.dismissViewControllerAnimated(true, completion: nil)


}
Shajakhan
  • 41
  • 2

0 Answers0