iPhone simulator plays video, real device won't

六月ゝ 毕业季﹏ 提交于 2019-12-20 03:20:22

问题


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?


回答1:


I might be reading this wrong, but you appear to be using a file located on your computer?




回答2:


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!




回答3:


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!

}



回答4:


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



来源:https://stackoverflow.com/questions/34265411/iphone-simulator-plays-video-real-device-wont

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!