Creating loop with avplayer

Good day, I watched a video by Chris on implementing Firebase Login/SignUp on YouTube. Here is the link: https://youtu.be/1HN7usMROt8

He used a video for the Login/SignUp page, a mp4 video. In the tutorial the video stops after one play. I want to know how to loop this video to keep playing when the user is on that page.

I will appreciate if anyone here can be kind to help.

Thank you.

Hi @OneRadioAfrica,

Is the class AVPlayerLooper() not what you are looking for?

1 Like

Yeah, I tried setting it up but it wasn’t working.

Here is my code:

import UIKit
import AVKit
import AVFoundation

class LoginSignUpViewController: UIViewController {
var videoPlayer:AVPlayer?
var videoPlayerLayer:AVPlayerLayer?
@IBOutlet weak var signUpButton: UIButton!
@IBOutlet weak var loginButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    setUpElements()
}

override func viewWillAppear(_ animated: Bool) {
     
     // Set up video in the background
     setUpVideo()
 }
func setUpElements() {
    
    Utilities.styleFilledButton(signUpButton)
    
    Utilities.styleFilledButton(loginButton)
    
}

 func setUpVideo() {
      
      // Get the path to the resource in the bundle
      let bundlePath = Bundle.main.path(forResource: "signup", ofType: "mp4")
      
      guard bundlePath != nil else {
          return
      }
      
      // Create a URL from it
      let url = URL(fileURLWithPath: bundlePath!)
      
      // Create the video player item
      let item = AVPlayerItem(url: url)
      
      // Create the player
      videoPlayer = AVPlayer(playerItem: item)
      
      // Create the layer
      videoPlayerLayer = AVPlayerLayer(player: videoPlayer!)
      
      // Adjust the size and frame
      videoPlayerLayer?.frame = CGRect(x: -self.view.frame.size.width*1.9, y: 0, width: self.view.frame.size.width*4, height: self.view.frame.size.height)
      
      view.layer.insertSublayer(videoPlayerLayer!, at: 0)
      
      // Add it to the view and play it
    videoPlayer?.playImmediately(atRate: 1.0)
  }

}

The following code will be the way to implement that function:

    var videoPlayer:AVPlayer?
    var videoPlayerLayer:AVPlayerLayer?
    var playerLooper:NSObject?
    
    override func viewWillAppear(_ animated: Bool) {
        setUpVideo()
    }
    
    func setUpVideo() {
        
        // Get the path to the resource in the bundle
        let bundlePath = Bundle.main.path(forResource: "signup", ofType: "mp4")
        guard bundlePath != nil else {
            return
        }
        
        // Create a URL from it
        let url = URL(fileURLWithPath: bundlePath!)
        
        // Create the video player item
        let item = AVPlayerItem(url: url)
        
        // Assign an array of 1 item to AVQueuePlayer
        videoPlayer = AVQueuePlayer(items: [item])
        
        // Loop the video
        playerLooper = AVPlayerLooper(player: videoPlayer! as! AVQueuePlayer, templateItem: item)
        
        // Create the layer
        videoPlayerLayer = AVPlayerLayer(player: videoPlayer)
        
        // Adjust the size and frame
        videoPlayerLayer!.frame = view.bounds
        videoPlayerLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
        view.layer.addSublayer(videoPlayerLayer!)
        
        // Add it to the view and play it
        videoPlayer?.play()
    }
1 Like

Hello Cedric06nice, thank you very much! It worked perfectly. I’m new to coding so I’m still rusty, but learning everyday.

Thanks again!

1 Like

Your welcome!
Please mark it as answered, so other can benefit in the future