The following Swift 5 function is what I'm using to display local MP4 video with native controls and looping at default. I was wondering if anyone had a more efficient way of incorporating a new function (e.g. AVPlayerLooper) to achieve the same functionality?
import Foundation
import AVFoundation
import AVKit
class VideoPlayer
{
public var VideoVC = AVPlayerViewController()
func playVideo(fileName:String, inView:UIView)
{
if let path = Bundle.main.path(forResource: fileName, ofType: "mp4")
{
let videoURL = URL(fileURLWithPath: path)
VideoVC.showsPlaybackControls = true
VideoVC.player = AVPlayer(url: videoURL)
inView.addSubview(VideoVC.view)
VideoVC.view.frame = inView.bounds
VideoVC.player?.isMuted = true
VideoVC.player?.play()
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.VideoVC.player?.currentItem, queue: .main)
{ [weak self] _ in
self?.VideoVC.player?.seek(to: CMTime.zero)
self?.VideoVC.player?.play()
}
}
}
}
Appreciate all insight in advance!