IPhone simulator plays video, the real device will not

I am building an application in Swift on Xcode 7.2

I added the video to my ViewController. Here is 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 I run the application in the simulator, but it will not work on a real device. What am I missing here?

+2
source share
4 answers

I may have read it incorrectly, but you seem to be using a file located on your computer?

+1
source

Thanks for all the tips. I created them:

 let path = NSBundle.mainBundle().pathForResource("video", ofType:"m4v") 

And they called them in action buttons:

 @IBAction func playButtonPressed(sender: AnyObject) { playerView = AVPlayer(URL: NSURL(fileURLWithPath: path!)) playerViewController.player = playerView self.presentViewController(playerViewController, animated: true){ self.playerViewController.player?.play()}} 

And he WORKS !!

Thanks!

+1
source

Because your path that you used is a simulator, it has changed on a real device. 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! } 
0
source

Maybe the problem is that the file names are case sensitive on the iPhone, but not in the simulator?

-1
source

Source: https://habr.com/ru/post/1489807/


All Articles