Loop MP4 With AVPlayerViewController In UIView

With version 3.8 of EchoTools, I’m using the following code to display ultrasound videos within an UIView element in Swift 5. I wanted to use AVPlayerViewController to have the native controls (mute, expand, etc.) while maintaining looping. Here’s the code.

class VideoPlayer
{
    public var VideoVC = AVPlayerViewController()
    private var playerLooper: AVPlayerLooper!
    
    func playVideo(fileName:String, vc:UIViewController, inView:UIView)
    {
        if let path = Bundle.main.path(forResource: fileName, ofType: "mp4")
        {
            let videoURL = URL(fileURLWithPath: path)
            VideoVC.showsPlaybackControls = true
            let playerItem = AVPlayerItem(url: videoURL)
            VideoVC.player = AVQueuePlayer(playerItem: playerItem)
            playerLooper = AVPlayerLooper(player: self.VideoVC.player as! AVQueuePlayer, templateItem: playerItem)
            vc.addChild(VideoVC)
            inView.addSubview(VideoVC.view)
            VideoVC.view.frame = inView.bounds
            VideoVC.player?.isMuted = true
            VideoVC.player?.play()
        }
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles