2017-05-27 153 views
1

在我的一个项目中,我从服务器下载了一个远程mp4视频,并将其保存在iOS设备(Swift 3,iOS 10)的“文档”文件夹中。 我试图发挥以多种方式使用AVPlayer本地视频(用URL,使用AVPlayerItem/AVAsset等),但我总是喜欢这样的画面结束:iOS 10上的Swift 3:如何从设备文档文件夹播放mp4/mov本地视频文件?

Video playing doesn't start

这是代码的最后一个版本,我试图用播放的文件:

let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) 
if let docDir : URL = urls.first { 
    let component = self.videoTutorialsEntries[indexPath.row].filename 
    let videoUrl = docDir.appendingPathComponent(component) 

    do { 
     let videoExists : Bool = try videoUrl.checkResourceIsReachable() 
     if videoExists {       
      let player = AVPlayer(url: videoUrl) 
      let playerController = AVPlayerViewController() 
      playerController.player = player 
      self.present(playerController, animated: true) { 
       playerController.player!.play() 
      } 
     } else { 
      print("Video doesn't exist") 
     } 
    } catch let error as NSError { 
     print(error) 
    } 
} 

我检查这些文件夹包含了视频,并且该URL /路径是正确的,它似乎很。

我也试图用故事板中的一个独立的AVKit视图控制器来处理这个问题,使用segue,但结果不会改变。

我想知道是否有必要,比如出于安全原因您必须在Info.plist上执行以播放远程文件的更改,但在这种情况下,它是本地文件,因此我缺乏创意。

有谁知道视频文件不播放的原因?

更新: 我才意识到,分析应用程序 (There's a way to access the document folder in iphone/ipad (real device, no simulator)?)的沙盒,我已保存的是某种损坏的文件(它应该是像22 MB,但在沙箱中,它只是一个几KB ,这意味着问题应该在保存过程中)。 我有我的Dropbox的远程文件,并将该文件保存使用此技术:

// Necessary when the remote server doesn't send the file size 
var request = URLRequest(url: self.videoUrl, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 30.0) 
request.httpMethod = "HEAD"; 
request.timeoutInterval = 5; 
let group = DispatchGroup() 
group.enter() 
URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in 
    if error != nil { 
     DispatchQueue.main.async(execute: { 
      self.showAlert(title: NSLocalizedString("Video Unreachable", comment: "Video Unreachable Title"), message: NSLocalizedString("It was not possible to download the video file!", comment: "Video Not Downloaded Title")) 
     }) 
    } else { 
     DispatchQueue.main.async(execute: { 
      self.contentLength = response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown 
      group.leave() 
      self.progressLabel?.center = CGPoint(x:UIScreen.main.bounds.size.width/2, y:(self.pointInTable.y/2)+40) 
      self.progressLabel?.isHidden = false 
     })           
     self.downloadVideo() 
    } 
}).resume() 

func downloadVideo() { 
    self.isVideo = true 
    downloadTask = backgroundSession.downloadTask(with: self.videoUrl) 
    downloadTask.resume() 
} 

因为我用这个异步处理也为PDF文件(这里下载的作品),我假设这个问题可以成为视频的保管箱链接。有关可能的问题的任何想法?

更新: 我终于成功地播放了该文件! 该问题与我使用的Dropbox url表单有关。 为了下载文件,你需要一个像这样的链接:与最终的“DL = 1?”这意味着你要下载的文件,而不仅仅是它想象 https://www.dropbox.com/s/xxxxxxxxxxxx/xxxxxx.mp4?dl=1

。我错过了。

最后,一切都与这段代码的工作:

let fileManager = FileManager.default 
let docsUrl = try! fileManager.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) 
let component = self.videoTutorialsEntries[indexPath.row].filename 
let fileUrl = docsUrl.appendingPathComponent(component) 
performSegue(withIdentifier: "showLocalVideo", sender: fileUrl) 

(我用从故事板和SEGUE视图控制器,只是网址(不需要的物品和资产)的

希望这有助于别人从Dropbox的节省远程视频和本地播放他们奋力

回答

0

尝试这样的:

let asset = AVAsset(URL: your_videoUrl) 
let item = AVPlayerItem(asset: asset) 
let player = AVPlayer(playerItem: item) 
+1

谢谢,我也试过资产和项目,但不幸的是结果没有改变。 – Thorh73

相关问题