简体   繁体   English

为什么我找不到使用AVPlayerViewController播放视频的方法

[英]Why I can't find a way to play video with AVPlayerViewController

Hello everybody 大家好

I've been stuck with this problem with a whole day and actually I don't find a way to play a video file, I will explain a little bit what I did: 我整天都被这个问题困扰着,实际上我找不到播放视频文件的方法,我将解释一下我做了什么:

I have the following Url: [https://rapidresponse-us-oem-storage11.mios.com/storage/storage/store/8157061/archive?Key=833101823][1] 我有以下网址:[https://rapidresponse-us-oem-storage11.mios.com/storage/storage/store/8157061/archive?Key=833101823][1]

If you could see in that url I'm getting a video file .mp4 and I need to play this video, the following is the code that I'm using, first I'm saving the file and after that i'm trying to play in the AVPlayerViewController: 如果您在该网址中看到我正在获取一个视频文件.mp4,并且需要播放此视频,则以下是我正在使用的代码,首先要保存该文件,然后再尝试在AVPlayerViewController中播放:

let store = self.eventDetail?.EventDetailStore!
let url = "https://" + (self.eventDetail?.EventDetailServerStorage)! + "/storage/storage/store/" + store! + "/archive?Key=" + self.EventKey!
if let videoUrl = NSURL(string: url) {
   // create your document folder url
   let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL
   // your destination file url
   let destinationUrl = documentsUrl.URLByAppendingPathComponent(videoUrl.lastPathComponent!)
   print(destinationUrl)
   // check if it exists before downloading it
   if NSFileManager().fileExistsAtPath(destinationUrl.path!) {
      print("The file already exists at path")
      let player = AVPlayer(URL: destinationUrl)
      let playerViewController = AVPlayerViewController()
      playerViewController.player = player
      self.presentViewController(playerViewController, animated: true) {
           playerViewController.player!.play()
      }

      } else {
         //  if the file doesn't exist
         //  just download the data from your url
         if let myAudioDataFromUrl = NSData(contentsOfURL: videoUrl){
             // after downloading your data you need to save it to your destination url
            if myAudioDataFromUrl.writeToURL(destinationUrl, atomically: true) {
               print(destinationUrl)
               let player = AVPlayer(URL: destinationUrl)
               let playerViewController = AVPlayerViewController()
               playerViewController.player = player
                            self.presentViewController(playerViewController, animated: true) {
                                playerViewController.player!.play()
                            }

                        } else {
                            let alert = UIAlertController(title: "Alert", message: "An error occurred", preferredStyle: UIAlertControllerStyle.Alert)
                            alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
                            self.presentViewController(alert, animated: true, completion: nil)
                        }
                    }
                }
            }

When I run the code i get the Video Controller but don't reproduce anything like no file exist, but the I debug the code and the file seems to be saved. 当我运行代码时,我得到了视频控制器,但没有像没有文件一样重现任何东西,但是我调试了代码,文件似乎已保存。

I will appreciate any help with this problem 我将感谢您对这个问题的任何帮助

Base on your suggestions I Updated the code, but it isn't work yet: 根据您的建议,我更新了代码,但尚无法解决:

//video
            var player: AVPlayer!
            var destinationUrl: NSURL? = nil
            dispatch_async(dispatch_get_global_queue(priority, 0)) {
                let store = self.eventDetail?.EventDetailStore!
                let url = "https://" + (self.eventDetail?.EventDetailServerStorage)! + "/storage/storage/store/" + store! + "/archive?Key=" + self.EventKey!
                if let videoUrl = NSURL(string: url) {
                    let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL
                    // your destination file url
                    destinationUrl = documentsUrl.URLByAppendingPathComponent(videoUrl.lastPathComponent!)
                    if NSFileManager().fileExistsAtPath(destinationUrl!.path!) == false {
                        if let myAudioDataFromUrl = NSData(contentsOfURL: videoUrl){
                            myAudioDataFromUrl.writeToURL(destinationUrl!, atomically: true)
                        }
                    }
                }
                dispatch_async(dispatch_get_main_queue()) {
                    if(destinationUrl != nil){
                        player = AVPlayer(URL: destinationUrl!)
                        let playerViewController = AVPlayerViewController()
                        playerViewController.player = player
                        self.presentViewController(playerViewController, animated: true, completion: { () -> Void in playerViewController.player!.play() })
                    }
                }
            }
        }

You're missing the completion handler parameter: 您缺少完成处理程序参数:

self.presentViewController(playerViewController, animated: true) {
                            () in playerViewController.player!.play()
                        }

Your player is going out of scope and is being deallocated before your video has finished playing. 您的播放器超出范围,并在视频播放完毕之前被释放。 Assign it to a class member to extend its lifetime. 将其分配给类成员以延长其寿命。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM