How to Play Video With Avplayerviewcontroller (Avkit) in Swift

How to play video with AVPlayerViewController (AVKit) in Swift

Swift 3.x - 5.x

Necessary: import AVKit, import AVFoundation

AVFoundation framework is needed even if you use AVPlayer

If you want to use AVPlayerViewController:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}

or just AVPlayer:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()

It's better to put this code into the method: override func viewDidAppear(_ animated: Bool) or somewhere after.


Objective-C

AVPlayerViewController:

NSURL *videoURL = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:^{
[playerViewController.player play];
}];

or just AVPlayer:

NSURL *videoURL = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:playerLayer];
[player play];

How to play a local video with Swift?

Sure you can use Swift!

1. Adding the video file

Add the video (lets call it video.m4v) to your Xcode project

2. Checking your video is into the Bundle

Open the Project Navigator cmd + 1

Then select your project root > your Target > Build Phases > Copy Bundle Resources.

Your video MUST be here. If it's not, then you should add it using the plus button

Sample Image

3. Code

Open your View Controller and write this code.

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
playVideo()
}

private func playVideo() {
guard let path = Bundle.main.path(forResource: "video", ofType:"m4v") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
present(playerController, animated: true) {
player.play()
}
}
}

Detect when AVPlayerViewController is closed

There is no specific delegate or notification that I know of for when the AVPlayerViewController is dismissed.

However, you could try the following steps since you present the AVPlayerViewController

1. Conformance to the UIViewControllerTransitioningDelegate

Make the UIViewController that presents the AVPlayerViewController conform to the transitioning delegate

class YourVC: UIViewController, UIViewControllerTransitioningDelegate

2. AVPlayerController SetUp

Make the view controller presenting the AVPlayerController the transitioningDelegate of the AVPlayerController

let vc = AVPlayerViewController()

// Add this
vc.transitioningDelegate = self

vc.player = player

3. Finally, implement the transition delegate function

// MARK: UIViewControllerTransitioningDelegate
// Gets called when a modal was dismissed
func animationController(forDismissed dismissed: UIViewController)
-> UIViewControllerAnimatedTransitioning?
{
// The dismissal was before the movie ended
print("AVPlayerController dismissed")
return nil
}

Give this a go and see if this helps

How to manage next and previous button in custom video player

Declare class variable var player = AVPlayer? and in you playVideo() function add self.player = player. When you want to replace video try this:

    func replaceVideo() {
let playerItem = AVPlayerItem(url: URL(string: "videoURL")!)
self.player.replaceCurrentItem(with: playerItem)
}


Related Topics



Leave a reply



Submit