gpt4 book ai didi

swift - AvPlayer 无法从自定义路径播放视频

转载 作者:行者123 更新时间:2023-11-28 06:44:59 24 4
gpt4 key购买 nike

我正在尝试播放我在此路径中录制的视频。但是我无法从这条路径播放视频。我该怎么办?

let pathMovie = (NSHomeDirectory() as NSString).stringByAppendingPathComponent("Documents/Movie.mp4")

let fileUrl = NSURL(fileURLWithPath: pathMovie)
self.videoFrame = view.frame
self.fillMode = .ResizeAspectFill
self.alwaysRepeat = true
self.sound = true
self.startTime = 0.0
self.alpha = 1
self.backgroundColor = UIColor.blackColor()
self.contentURL = fileUrl

这是我得到的错误。

Failed: Optional(Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12893), NSLocalizedDescription=The operation could not be completed, NSURL=file:///var/mobile/Containers/Data/Application/.....-...-...-....-..../Documents/Movie.mp4, NSUnderlyingError=0x150a38e80 {Error Domain=NSOSStatusErrorDomain Code=-12893 "(null)"}})

最佳答案

您可以通过添加以下两种方法从自定义路径播放视频:

注意:请将您的视频名称作为参数传递给 getCustomVideoPath 方法。

swift 4

func getCustomVideoPath(name: String) -> String {
let strDocumentDirectory = self.getDocumentDirectoryPath()
return "\(strDocumentDirectory)/\(name).mp4"
}

func getDocumentDirectoryPath() -> String {
let arrPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
return arrPaths[0]
}

//----------------------------------------------------------------

func playCustomVideo() {
let strVideoPath = self.getCustomVideoPath(name:"Movie")
let urlVideoPath = URL(fileURLWithPath: strVideoPath)
let newPlayerItem = AVPlayerItem(url: urlVideoPath)
self.avPlayer = AVPlayer(playerItem: newPlayerItem)
avPlayerController.player = self.avPlayer
self.avPlayer?.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(rawValue: 0), context: MPPlayableContentManagerContext.observationInfo())
self.present(avPlayerController, animated: true) {
self.avPlayer?.play()
}
}

关于swift - AvPlayer 无法从自定义路径播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36725125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com