从 Parse 下载后无法流式传输视频

Posted

技术标签:

【中文标题】从 Parse 下载后无法流式传输视频【英文标题】:Impossible to stream video after downloading from Parse 【发布时间】:2016-06-01 08:21:31 【问题描述】:

从 Parse 后端下载视频后,我似乎无法流式传输视频。但是我知道这些视频运行良好,因为您可以从 Chrome 播放它,但不能从 Safari 播放(我不知道为什么......)。

我在 AVPlayer 上播放视频的代码也是正确的,因为我测试了本地数据库中的多个 NSUrl。

几篇关于它的帖子证实存在问题,但没有人给出答案。

ios - Can't stream video from Parse Backend

Cant stream video (PFFile) from parse server

如果有人可以帮助我,请提前感谢。

【问题讨论】:

【参考方案1】:

秘诀是获取 PFFIle 数据,以适当的扩展名 (mov) 保存在本地并处理本地文件。 看看这个条目[Cant stream video (PFFile) from parse server][1],第二个答案。

PFFIle 是使用以下代码创建的:

let videoPath = info[UIImagePickerControllerMediaURL] as! NSURL


      let imageData = NSData (contentsOfURL:videoPath)! as NSData

            do  

              let file = try PFFile(name: "_mov", data: imageData, contentType: "video/quicktime")
              videoSaveSelected(file )
             catch let error as NSError 
              print("Error generating PFFile: \(error)")
            

这是播放器的代码

import UIKit
import AVKit
import MobileCoreServices
import AssetsLibrary
import AVFoundation
import Parse
import ParseUI

class VideoPlayerViewController: UIViewController 

  var videoUrl: NSURL!
  var file: PFFile!

    override func viewDidLoad() 
        super.viewDidLoad()


      // navigation
      let backButtonCustom = UIButton( frame: CGRectMake(0, 0, 22, 22))
      let backIcon = UIImage(named: "0836-arrow-left")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
      backButtonCustom.setImage(backIcon, forState: UIControlState.Normal)
      backButtonCustom.addTarget(self, action: #selector(ReqDataEntryLgViewController.doneButtonTapped), forControlEvents: UIControlEvents.TouchUpInside)
      navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButtonCustom)

      let doneButton = UIBarButtonItem(title: "Done", style: .Done, target: self, action: #selector(ReqDataEntryLgViewController.doneButtonTapped))
      self.navigationItem.setRightBarButtonItems([doneButton], animated: true)




      // video player

      let playerController = AVPlayerViewController()

      self.addChildViewController(playerController)
      self.view.addSubview(playerController.view)
      playerController.view.frame = self.view.frame

      // get data from PFFile in database
      file!.getDataInBackgroundWithBlock(
        (movieData: NSData?, error: NSError?) -> Void in
        if (error == nil) 

                    let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
          let destinationPath:NSString = documentsPath.stringByAppendingString("/file.mov")

          let filemanager = NSFileManager.defaultManager()
          do 
             try filemanager.removeItemAtPath(destinationPath as String)
           catch  
            print("Ooops! Something went wrong: \(error)")
          

          // write to local file
          movieData!.writeToFile ( destinationPath as String, atomically:true)

//          do 
//            let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(destinationPath as String)
//            
//            if let _attr = attr 
//              let fileSize = _attr.fileSize()
//              print ("Movie Size \(fileSize)" )
//            
//           catch 
//            print("Ooops! Something went wrong getting size: \(error)")
//          

          let playerItem = AVPlayerItem(asset: AVAsset(URL: NSURL(fileURLWithPath: destinationPath as String)))
          let player = AVPlayer(playerItem: playerItem)
          playerController.player = player
          player.play()
         else 
          print ("error on getting movie data \(error?.localizedDescription)")
        
      )




  


  func doneButtonTapped()

    navigationController?.popViewControllerAnimated(true)
  

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    




【讨论】:

以上是关于从 Parse 下载后无法流式传输视频的主要内容,如果未能解决你的问题,请参考以下文章

Android VideoView 流式传输

获取 BrightCove 视频的 URL 以供下载和流式传输

下载 iOS 时流式传输视频

如何使用 html 5 视频元素流式传输视频

Android 从 Google 驱动器流式传输视频

以编程方式将视频从 php 服务器流式传输到 android