0

我正在尝试播放保存在文档目录中的视频 (.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)
    }
} 
4

0 回答 0