Sunday, February 15, 2026
HomeiOS DevelopmentMethods to play video in swift(AVPlayerViewController) – iOSTutorialJunction

Methods to play video in swift(AVPlayerViewController) – iOSTutorialJunction

[ad_1]

Enjoying a video in swift isn’t a really advanced job, however because of frameworks present by apple. AVKit framework offers a excessive degree participant interface for developer’s to play video in swift language. AVPlayerViewController’s participant is able to taking part in video, both from file saved regionally 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 provided 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 isn’t discovered then we is not going to play video.

guard let filePath = Bundle.primary.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 methods to play video in swift utilizing AVPlayerViewController of AVKit framework in ios sdk. Video may be performed utilizing a neighborhood video file and in addition 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

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments