[ad_1]
Apple supplied AVKit framework, to permit builders play multimedia content material inside their apps. With AVPlayerViewController, we are able to use Apple default playback controls. However in case if we wish to play video in our custom-made participant, then AVPlayer class involves rescue. On this tutorial, we are going to discover ways to play video utilizing AVPlayer in swift. You’ll be able to test how you can play video utilizing AVPlayerViewController from right here
Creating video participant in swift
On this tutorial we are going to create, a customized class inherited from UIView and add our AVPlayer to it. You’ll be able to discover ways to create customized UIView class in swift from under hyperlink
Create customized UIView class with xib in swift
Creating UIView class for video participant
Create a brand new file in xcode and choose cocoa contact class, identify it VideoPlayer. This class ought to be subclass of UIView as proven in under picture

Now we have to create a xib file for our participant, so create a brand new file in xcode, however this time we have to choose view class underneath Person Interface part. We are going to identify it “VideoPlayer“.

Steps to create customized video participant utilizing AVPlayer
1. Open VIdeoPlayer.xib and add a UIView object from object library on to the view. Add constraint’s main, trailing, high, backside having fixed worth 0 with respect to superview.
2. In VideoPlayer.swift create an IBOutlet of UIView and identify it as vwPlayer
class VideoPlayer: UIView { @IBOutlet weak var vwPlayer: UIView! }
3. Join IBOutlet created in step 2 with view object in VideoPlayer.xib.
4. Open VideoPlayer.swift, now we are going to first import AVKit framework and create an occasion of AVPlayer
import UIKit import AVKit class VideoPlayer: UIView { @IBOutlet weak var vwPlayer: UIView! var participant: AVPlayer? }
5. Add required intializer’s to our class. And in addition create a operate that will probably be chargeable for getting view from VideoPlayer.xib class.
import UIKit import AVKit class VideoPlayer: UIView { @IBOutlet weak var vwPlayer: UIView! var participant: AVPlayer? override init(body: CGRect) { tremendous.init(body: body) commonInit() } required init?(coder: NSCoder) { tremendous.init(coder: coder) commonInit() } fileprivate func commonInit() { let viewFromXib = Bundle.important.loadNibNamed("VideoPlayer", proprietor: self, choices: nil)![0] as! UIView viewFromXib.body = self.bounds addSubview(viewFromXib) } }
Utilizing AVPlayer inside VideoPlayer.swift
6. Create a brand new operate that can initialize AVPlayer occasion and, add participant to VideoPlayer as a sub view. Body of AVPlayer will probably be identical as per of our view object of VideoPlayer class.
fileprivate func addPlayerToView(_ view: UIView) { participant = AVPlayer() let playerLayer = AVPlayerLayer(participant: participant) playerLayer.body = self.bounds playerLayer.videoGravity = .resizeAspectFill view.layer.addSublayer(playerLayer) }
7. We are going to name our addPlayerToView operate from inside commonInit operate. As a result of participant ought to get intialized and added as quickly as an object of VideoPlyer will get created.
8. Subsequent, we are going to create a operate named playVideoWithFileName. Operate settle for two parameters, file identify as one in every of its parameter and different parameter will probably be file sort. On this operate, first participant will attempt to get file path of the filename participant was requested to play. Secondly, if participant discovered the file, it’s going to will get performed by participant.
func playVideoWithFileName(_ fileName: String, ofType sort:String) { guard let filePath = Bundle.important.path(forResource: fileName, ofType: sort) else { return } let videoURL = URL(fileURLWithPath: filePath) let playerItem = AVPlayerItem(url: videoURL) participant?.replaceCurrentItem(with: playerItem) participant?.play() }
Get notified when participant stops taking part in video
Until now, we efficiently added code that can play video from the file laid out in playVideoWithFileName operate. So, query arises how the system will get to know as quickly as participant end taking part in video. Reply to final query is utilizing AVPlayerItemDidPlayToEndTime notification. Notification observer will probably be added to operate addPlayerToView and thus permitting VideoPlayer to hear for AVPlayer finish taking part in notification.
NotificationCenter.default.addObserver(self, selector: #selector(playerEndPlay), identify: .AVPlayerItemDidPlayToEndTime, object: nil) @objc func playerEndPlay() { print("Participant ends taking part in video") }
Full code for VideoPlayer.swift
import UIKit import AVKit class VideoPlayer: UIView { @IBOutlet weak var vwPlayer: UIView! var participant: AVPlayer? override init(body: CGRect) { tremendous.init(body: body) commonInit() } required init?(coder: NSCoder) { tremendous.init(coder: coder) commonInit() } fileprivate func commonInit() { let viewFromXib = Bundle.important.loadNibNamed("VideoPlayer", proprietor: self, choices: nil)![0] as! UIView viewFromXib.body = self.bounds addSubview(viewFromXib) addPlayerToView(self.vwPlayer) } fileprivate func addPlayerToView(_ view: UIView) { participant = AVPlayer() let playerLayer = AVPlayerLayer(participant: participant) playerLayer.body = self.bounds playerLayer.videoGravity = .resizeAspectFill view.layer.addSublayer(playerLayer) NotificationCenter.default.addObserver(self, selector: #selector(playerEndPlay), identify: .AVPlayerItemDidPlayToEndTime, object: nil) } func playVideoWithFileName(_ fileName: String, ofType sort:String) { guard let filePath = Bundle.important.path(forResource: fileName, ofType: sort) else { return } let videoURL = URL(fileURLWithPath: filePath) let playerItem = AVPlayerItem(url: videoURL) participant?.replaceCurrentItem(with: playerItem) participant?.play() } @objc func playerEndPlay() { print("Participant ends taking part in video") } }
Utilizing customized video participant class inside important mission
Open Important.storyboard and drag a UIView object to it. Add constraints to it and at last change UIView class identify to VideoPlayer from Identification inspector.

In ViewController.swift, create IBOutlet of VideoPlayer and join this IBOutlet with VideoPlayer object added to storyboard. From viewDidLoad operate, name playVideoWithFileName operate. That’s it.
class ViewController: UIViewController { @IBOutlet weak var participant:VideoPlayer! override func viewDidLoad() { tremendous.viewDidLoad() // Do any further setup after loading the view. participant.playVideoWithFileName("videoTutorial1", ofType: "mp4") } }
[ad_2]