Playing a Video File from a Server in Swift

Playing a video file from a server in Swift

UPDATE 2019, Swift 4:

MPMovieControlStyle' was deprecated in iOS 9.0: Use AVPlayerViewController in AVKit.

So, following the advice, let's change it. Original answer from 2016 is below.

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

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

let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!

playVideo(url: url)
}

func playVideo(url: URL) {
let player = AVPlayer(url: url)

let vc = AVPlayerViewController()
vc.player = player

self.present(vc, animated: true) { vc.player?.play() }
}
}

The original answer:

import UIKit
import MediaPlayer

class VideoViewController: UIViewController {

var moviePlayer:MPMoviePlayerController!

override func viewDidLoad() {
super.viewDidLoad()

let url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")

moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)

self.view.addSubview(moviePlayer.view)
moviePlayer.fullscreen = true

moviePlayer.controlStyle = MPMovieControlStyle.Embedded

}
}

How to play a video from either a local or a server URL in iOS

1.First of all add MediaPlayer.Framework in XCode

2.Then add #import < MediaPlayer/MediaPlayer.h > in your viewController's .h file

3.Now implement this code in your viewDidLoad Method

     //NSString *filepath = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"mp4"];  
//NSURL *fileURL = [NSURL fileURLWithPath:filepath];

NSURL *fileURL = [NSURL URLWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];

moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];

For Orientation Please add this code

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
} else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[moviePlayerController.view setFrame:CGRectMake(0, 0, 480, 320)];
}
return YES;
}

In this code moviePlayerController is MPMoviePlayerController declared in .h file

How to access the Video files, which are present in iOS Files App using AVPlayer in iOS swift?

So as stated in error "No such file or directory" a file is absent. Regarding full path it stored inside temporary folder. Probably you imported these files via UIDocumentPickerViewController or similar component. You must copy provided file immediately (synchronously) in same code block where you get the URL to some local storage to avoid this situation.

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

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)
...


Related Topics



Leave a reply



Submit