How to Play a Local Video With Swift

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()
}
}
}

Play Local Video File

You have forgot to set playercontroller's player instance as phuc-nguyen said

or else you can make use of AVPlayerLayer to play the video without using AVPlayerViewController

let player = AVPlayer(url: URL(string: "path/myvideo.mp4")!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()

Using AVPlayerLayer you can customize the player like adding your own controls or views and it won't be a full screen view usually though.

But if you are not looking for any customization its good to stick with AVPlayerViewController it has all the controls baked in

Depends on your requirement

how to play a video in UIView swift?

the only way to add a video to a UIView with fixed constraints in storyboard, was this :

let url = URL(string:myURL)

player = AVPlayer(url: url!)

avpController.player = player

avpController.view.frame.size.height = videoView.frame.size.height

avpController.view.frame.size.width = videoView.frame.size.width

self.videoView.addSubview(avpController.view)

I hope other can use this! :)

Swift playing Videos From Local Path

Do not use a literal string path. Get the current Documents folder with FileManager:

let documentsFolder = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let videoURL = documentsFolder.appendingPathComponent("MyDownloads/Arrow.S05E01.mp4")
let player = AVPlayer(url: videoURL)
...

Play a local video automatically in in a UIView

In swift 3.0, try this code.

//for Playing Video

@IBAction func btnvideoPlayClicked(_ sender: UIButton) {

     self.videoPlay()
}

func videoPlay()
{
let playerController = AVPlayerViewController()
playerController.delegate = self

let bundle = Bundle.main
let moviePath: String? = bundle.path(forResource: "SoSorry", ofType: "mp4")
let movieURL = URL(fileURLWithPath: moviePath!)

let player = AVPlayer(url: movieURL)
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame

player.play()

}

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 local video in AVPlayer using an array of URL's?

[URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallBBw:Pound.mp4")

is not how you get a file within your bundle..

It's let path = Bundle.main.path(forResource: "Pound", ofType: "mp4")

Example: Add a "video" to your project with .mp4 format. Select the video and in the right panel, make the target your app. This will add the video to your Main-Bundle.

In your story board, add a button. Add an AVPlayerController. Control + Click that button and drag it to the AVPlayerController.

Sample Image

In the View Controller code, override the function prepareForSegue. Get its destination and set it's player to play the video by grabbing the Path to the video..

Sample Image
Sample Image
Sample Image

The code looks like:

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as! AVPlayerViewController

let url = URL(fileURLWithPath: Bundle.main.path(forResource: "Video", ofType: "mp4")!)
destination.player = AVPlayer(playerItem: AVPlayerItem(url: url))
destination.player?.play()
}

}

When you run the code and press the button, it will play the video. Tested it myself on an iPhone 6S, iOS 11.

How do I add a local video ? SWIFTUI

Replace this line: guard let url = URL(string: "https://video.mp4") else { return }

With this:

guard let url = guard let url = Bundle.main.url(forResource: "video", withExtension: ".mp4") { return }

(Assuming your file is named video.mp4 and is added to your project and target.



Related Topics



Leave a reply



Submit