PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 29, 2022

[FIXED] How can I make my video loop in swift with playerViewController?

 September 29, 2022     avplayerviewcontroller, swift, tvos, video, xcode     No comments   

Issue

I am trying to loop a video with playerViewController on Swift for TVOS. I have the video playing fine, but I want to loop the video. Here is my code so far:

override func viewDidAppear(_ animated: Bool) {
    let videoURL = URL(string: "https://url-to-video.com/video.mp4")
    let player = AVPlayer(url: videoURL!)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player        self.present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }
}

Any help is appreciated. Thanks


Solution

The quickest way to do this is to use an AVQueuePlayer with an AVPlayerLooper. You can set the player on your player view controller the same as you would with an ordinary AVPlayer, but you need to keep a persistent reference around to the looper so it’ll keep working. In other words, add this to your view controller’s interface:

var looper: AVPlayerLooper?

…and in your viewDidAppear, replace this:

let player = AVPlayer(url: videoURL!)

with this:

let player = AVQueuePlayer()
looper = AVPlayerLooper(player: player, templateItem: AVPlayerItem(asset: AVAsset(url: videoURL!)))

Then, once you start the player playing, its video will loop indefinitely.



Answered By - Noah Witherspoon
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing