播放保存在文档目录中的视频

时间:2018-06-20 05:13:00

标签: swift avplayer video-player avkit document-directory

我正在尝试播放保存在文档目录中的视频(.mov),但失败了。视频的URL已保存到核心数据中。视频文件保存在文档目录中(我通过将目录下载到我的Mac上进行了检查),并且保存了通过iPhone录制的视频并将其另存为“ .MOV”。视频在那里。返回的代码中的“ videourl”为“ file:///var/mobile/Containers/Data/Application/E45E8260-EA7C-4D30-91AD-D65A208D2057/Documents/myvideo.MOV”。 我想在用户按下按钮时播放视频。按下按钮后,将显示AVplayer,但会播放视频。 请帮助我。

@IBAction func buttonTwoInVmDetailForWatchingAndEditing(_ sender: UIButton) {
    print("***********************************")
    print(vmIDInWatchingAndEditingViewController)
    let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "TaskAnalysisURL")
    let predicateWithVmIDInVideoPhotoView = NSPredicate(format: "vmID == %@", vmIDInWatchingAndEditingViewController as CVarArg)
    fetchRequest.predicate = predicateWithVmIDInVideoPhotoView

    do { //try context.fetch(Users.fetchRequest())
        let taskAnalysisURLsSelected = try context.fetch(fetchRequest)
        let taskAnalysisURLsSelectedTofetch = taskAnalysisURLsSelected as! [TaskAnalysisURL]

        if taskAnalysisURLsSelectedTofetch.count == 0 {
            print("================== if ================")
            print(taskAnalysisURLsSelectedTofetch.count)
        } else {
            print("================== else ================")
            print(taskAnalysisURLsSelectedTofetch.count)
            for taskAnalysisURLSelectedTofetch in taskAnalysisURLsSelectedTofetch as [NSManagedObject] {
                let stepTwoURLToPlayVideo = taskAnalysisURLSelectedTofetch.value(forKey: "stepTwoURL")
                print("===========URL=============")
                print(stepTwoURLToPlayVideo!)

                let videoUrl = "file://\(String(describing: stepTwoURLToPlayVideo!))"
                print(videoUrl)
                let videourl = URL(fileURLWithPath: stepTwoURLToPlayVideo! as! String)
                print("@@@@@@@@@@@@@@@@@@@@@@@@@")
                print(videourl)

                let player = AVPlayer(url: videourl)  // video path coming from above function
                let playerViewController = AVPlayerViewController()
                playerViewController.player = player

                self.present(playerViewController, animated: true) {
                    let playerItem = AVPlayerItem(url: videourl)
                    let playerTwo = AVPlayer(playerItem: playerItem)
                    let playerLayer = AVPlayerLayer(player: playerTwo)
                    playerLayer.frame = self.view.frame
                    self.view.layer.addSublayer(playerLayer)

                    playerViewController.player!.play()
                }
            }
        }
    }
    catch {
        print(error.localizedDescription)
    }
} 

0 个答案:

没有答案