swiftavplayeravplayerlayer

Start video from certain point Swift


I have a video that plays within an image view using avPlayerLayer.

It pauses/plays when videoViewTapped is called (i.t. the imageView is tapped by the user). There is also an option for go in full screen mode.

I have tried using timeStopped = player.currentTime() to register the time the user selects the full-screen option and then use this point to start the video when in full screen.

Currently when I go full screen, the video restarts from the beginning and freezes after a couple of seconds, not at the point the video was when full screen mode is called. Below is my code:

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController  {

    @IBOutlet weak var videoView: UIImageView!
    @IBOutlet var imageView: UIImageView!
    let videoPlayer = AVPlayerViewController()
    var player : AVPlayer!
    var avPlayerLayer : AVPlayerLayer!
    var timeStopped = CMTime()

    override func viewDidLoad() {
        super.viewDidLoad()
            guard let path = Bundle.main.path(forResource: "SampleVideo", ofType:"mp4") else {
                debugPrint("Logo-Animation4.mp4 not found")
                return
            }
            player = AVPlayer(url: URL(fileURLWithPath: path))
            avPlayerLayer = AVPlayerLayer(player: player)
            avPlayerLayer.videoGravity = AVLayerVideoGravity.resize

            videoView.layer.addSublayer(avPlayerLayer)
        }    

    //MARK: VIDEO STUFF
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        avPlayerLayer.frame = videoView.layer.bounds
    }


    @IBAction func videoViewTapped(_ sender: UITapGestureRecognizer) {
        if videoView.tag == 0 {
            player.play()
            videoView.tag = 1
        } else {
            player.pause()
            timeStopped = player.currentTime()
            videoView.tag = 0
        }
    }

    @IBAction func fullScreen(_ sender: Any) {
        videoPlayer.player = player

        present(videoPlayer, animated: true) {
            self.videoPlayer.showsPlaybackControls = true
        }
    }
}

Is anyone able to point out my error?


Solution

  • You can also use the AVPlayer from videoPlayer so you won't have to reassign your player when user enters full screen.

    let videoPlayer = AVPlayerViewController()
    
    
     override func viewDidLoad() {
        super.viewDidLoad()
    
            videoPlayer.player = AVPlayer(url: URL(fileURLWithPath: path))
    
        }
    
    
      @IBAction func videoViewTapped(_ sender: UITapGestureRecognizer) {
        if videoView.tag == 0 {
            videoPlayer.player.play()
            videoView.tag = 1
        } else {
            videoPlayer.player.pause()
    
            videoView.tag = 0
        }
    }
    
    @IBAction func fullScreen(_ sender: Any) {
    
    
        present(videoPlayer, animated: true) {
            self.videoPlayer.showsPlaybackControls = true
            videoPlayer.player.play()
        }
    }