The right way to play video in swift(AVPlayerViewController) – iOSTutorialJunction

0
30


Taking part in a video in swift is just not a really advanced activity, however due to frameworks present by apple. AVKit framework offers a excessive stage participant interface for developer’s to play video in swift language. AVPlayerViewController’s participant is able to enjoying video, both from file saved domestically contained in the app bundle or from a file hosted on distant URL.

Steps to play video utilizing AVPlayerViewController

AVPlayerViewController provides developer capability to make use of system default participant as system participant is equipped with in constructed playback controls. Since AVPlayer of AVPlayerViewController has playback controls. So developer’s want to not fear of doing additional coding for dealing with video playback.

On this tutorial we, will learn to play video in swift utilizing AVPlayerViewController.

1: Import AVKit framework

import UIKit
import AVKit

2: Get file path of the video you need to play in AVPlayer. We’ll use guard assertion as a result of if file is just not discovered then we is not going to play video.

guard let filePath = Bundle.essential.path(forResource: "A", ofType: "mp4") else { return}

3: Create an occasion of AVPlayer utilizing the above file path.

  let participant = AVPlayer(url: URL(fileURLWithPath: filePath))

4: Subsequent, create an occasion of AVPlayerViewController

 let vc = AVPlayerViewController()

5: Assign participant created in step 3, to AVPlayerViewController’s property named participant.

6: Lastly, current AVPlayerViewController on to the display.

current(vc, animated: true)

Output:

Video getting played inside AVPlayerViewController-play video in swift

The place to go from right here

On this tutorial, we discovered tips on how to play video in swift utilizing AVPlayerViewController of AVKit framework in ios sdk. Video will be performed utilizing an area video file and likewise from a url for the video file hosted on distant server.

Extra tutorial on swift

Property Observers in swift

Create customized UIView class with xib in swift