Adding Local files to projects files - ios

import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerviewcontroller = AVPlayerViewController()
var playerview = AVPlayer ()
#IBAction func playMusic(sender: AnyObject) {
let fileURL = NSURL(fileURLWithPath:"/Users/MorganEvans/Documents/Apps/32134.mp4")
playerview = AVPlayer(URL: fileURL)
playerviewcontroller.player = playerview
self.presentViewController(playerviewcontroller, animated: true){
self.playerviewcontroller.player?.play()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
When I use the app it does not work because the file is on my mac. Please Can someone explain how I put the video file in the project. Thanks

You just need to drag and drop the file like video or image that you want to use in your project statically. Following i just add :
#IBAction func playMusic(sender: AnyObject) {
let path = NSBundle.mainBundle().pathForResource("32134", ofType:"mp4")
let fileURL = NSURL(fileURLWithPath: path)
playerview = AVPlayer(URL: fileURL)
playerviewcontroller.player = playerview
self.presentViewController(playerviewcontroller, animated: true){
self.playerviewcontroller.player?.play()
}
}
Another way is Past your video, audio or image file in your project folder like following:
after that Add this file in to your xcode project by following step:
You get drapDown window and select your file as following:
And you the same code for getting file path:
let path = NSBundle.mainBundle().pathForResource("32134", ofType:"mp4")
let fileURL = NSURL(fileURLWithPath: path)

Try checking for the nil. Also get the video file URL from NSBundle
if let path = NSBundle.mainBundle().pathForResource("32134", ofType:"mp4") {
let fileURL = NSURL(fileURLWithPath: path!)
playerview = AVPlayer(URL: fileURL)
playerviewcontroller.player = playerview
self.presentViewController(playerviewcontroller, animated: true){
self.playerviewcontroller.player?.play()
}
}

Related

AVPlayer is not playing a locally saved video file

I have this code attempting to play a previously saved file (which exists) but I see a black screen while playing the video.
static func playVideo(from filename: String, vc: UIViewController, view: UIView) -> Bool {
let filepath = videosDirectoryPath + filename
if (MediaUtils.fileExists(filePath: filepath)) {
Logging.logError("Playing video failed, file not found: \(filepath)")
return false
}
let player = AVPlayer(url: URL(fileURLWithPath: filepath))
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
vc.present(playerViewController, animated: true) {
player.play()
}
return true
}
I do see an error Unbalanced calls to begin/end appearance transitions for <AVPlayerViewController: 0x109010e00>. but not sure if that has anything to do here. I am able to play a URL fine with this:
static func playVideo(videoUrl: String, vc: UIViewController, view: UIView) {
Logging.logDebug("Playing video \(videoUrl)")
let player = AVPlayer(url: URL(string: videoUrl)!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
vc.present(playerViewController, animated: true) {
player.play()
}
}
Any idea why the locally saved file cannot be played. Wish there is an error from player.play().
You can give this a shot 😉
func playVideo(videoUrl: String, vc: UIViewController, view: UIView) {
let player = AVPlayer(url: NSURL(fileURLWithPath: videoUrl) as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspect
playerLayer.frame = logoImageView.frame
view.layer.addSublayer(playerLayer)
vc.present(playerViewController, animated: true) {
player?.play()
}
}

why video is not getting played only showing loader in swift

I am trying to play the video from the web url it showing buffering loader only but not getting played I have used the following code.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let url = URL(string: "https://sandbox-api.digiboxx.com/uploads/E2D6024483AB4B04/1602945683_sample_640x360.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() }
}
I tried your URL just in the browser (Safari) on my computer and nothing plays. So I think something is wrong with the URL or the file at the other end.
I tested your link and the video is loading, so I don't know why your code isn't working ...
This is how I use VideoPlayer in Swift :
import UIKit
import AVKit
class MediaManager: UIViewController {
var player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
play(url: "yourUrl")
}
func play (url: String) {
player = AVPlayer(url: URL(string: url)!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.view.present(playerViewController, animated: false) { self.player.play() }
}
}
Try with my code and tell me if it's okay for you

Stop AV Player after done is pressed

I get my local video to load into a new view controller, but after hitting done it disappears and immediately pops back up again. I don't know if there is a way to track the notifications or what.
Here is my code:
import UIKit
import AVKit
import AVFoundation
class VideoController1: UIViewController {
var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
let fileURL = NSURL(fileURLWithPath: "/Users/jamesjarrett/Desktop/NIMS/NIMS/NIMS/Vid2.mp4")
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
self.playerViewController.player?.play()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You put your code
let fileURL = NSURL(fileURLWithPath: "/Users/jamesjarrett/Desktop/NIMS/NIMS/NIMS/Vid2.mp4")
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
self.playerViewController.player?.play()
}
inside of ViewDidAppear for the ViewController.
ViewDidAppear() occurs when your view is presented on the screen again. So this means, that when the AVPlayer is closed, it goes back to that View, and the AVPlayer will be presented again.
You have 2 options:
Make a UIButton and an Action to that button
var button = UIButton(frame: CGRect(0, self.view.frame.height*0.9, self.view.frame.width, self.view.frame.height*0.1))
button.addTarget(self, #selector(VideoController1.playController), nil)
self.view.addSubview(button)
let fileURL = NSURL(fileURLWithPath: "/Users/jamesjarrett/Desktop/NIMS/NIMS/NIMS/Vid2.mp4")
func playController() {
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
self.playerViewController.player?.play()
}
}
-- OR --
If you want the controller to play only the first time the user sees the view, and that is it, then you can do
var played = false
func viewDidAppear() {
if(!played) {
let fileURL = NSURL(fileURLWithPath: "/Users/jamesjarrett/Desktop/NIMS/NIMS/NIMS/Vid2.mp4")
playerView = AVPlayer(url: fileURL as URL)
playerViewController.player = playerView
self.present(playerViewController, animated: true){
played = true
self.playerViewController.player?.play()
}
}
}
Don't quote me on the code above, just typed it up real quick to give you a general idea.

I can't load video file from local server in swift

I'm working with iOS using Swift.
I want to load a video file using AVPlayer in Swift.
I was able to load an image and a text file from the local server. But I couldn't load a video file. This is the code that I'm using.
let movieUrl = NSURL(string: "http://akjfsjf/ajdfl/ajkf/car.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
I have tried with following URL and function
func playvideo (){
let movieUrl = NSURL(string: "http://www.w3schools.com/html/movie.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
player.play()
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
}
Also from your code i found you are using http rather than https so you need to add following property in plist file.
Try this one,
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var playerVC : AVPlayerViewController!
var playerItem : AVPlayerItem!
var player : AVPlayer!
var playerLayer: AVPlayerLayer!
override func viewDidAppear(animated: Bool) {
let url = NSURL.init(fileURLWithPath: "your file path")
self.playerItem = AVPlayerItem.init(URL: url!)
self.player = AVPlayer.init(playerItem: self.playerItem)
self.playerVC = AVPlayerViewController.init();
self.playerVC.player = self.player;
self.player.currentItem!.playbackLikelyToKeepUp
self.presentViewController(self.playerVC, animated: true) { () -> Void in
self.playerVC.player?.play()
}
}
}
Hope this helps!

iPhone simulator plays video, real device won't

I am building an app in Swift on Xcode 7.2
I added a video to my ViewController. Here's the code:
import UIKit
import AVKit
import AVFoundation
class GymViewController: UIViewController {
var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
let fileFemaleURL = NSURL(fileURLWithPath:"/Users/marienkoen/Documents/AppDevelopment/ROZsport/ROZsport/GymFemale-2434.m4v")
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func playButtonPressed(sender: AnyObject) {
playerView = AVPlayer(URL: fileFemaleURL)
playerViewController.player = playerView
self.presentViewController(playerViewController, animated: true){
self.playerViewController.player?.play()
}
I can play this video when running the app in the simulator, but it will not work on a real device. What am I missing here?
I might be reading this wrong, but you appear to be using a file located on your computer?
Thanks for all the hints. I created these:
let path = NSBundle.mainBundle().pathForResource("video", ofType:"m4v")
And called them in the button action:
#IBAction func playButtonPressed(sender: AnyObject) {
playerView = AVPlayer(URL: NSURL(fileURLWithPath: path!))
playerViewController.player = playerView
self.presentViewController(playerViewController, animated: true){
self.playerViewController.player?.play()}}
And it WORKS!!
Thanks!
Because your path you was using is from simulator, on real device it changed.
if you added this file to your project, you can get this url via:
func getDocumentsURL() -> NSURL {
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
return documentsURL
}
func fileInDocumentsDirectory(filename: String) -> String {
let fileURL = getDocumentsURL().URLByAppendingPathComponent(filename)
return fileURL.path!
}
Maybe the problem is that filenames are case-sensitive on the iPhone, but not in the simulator?

Resources