我正在尝试创建一个 AVURLAsset,如下所示:
class TrimFootageViewController: UIViewController {
var movieURL:URL?
override func viewWillAppear(_ animated: Bool) {
playerView.playerLayer.player = player
super.viewWillAppear(animated)
self.thumbnailImage = setThumbnailFrom(path: movieURL!)
print(type(of: self.movieURL!))
asset = AVURLAsset(url: self.movieURL!, options: nil)
print(asset ?? "couldn't get asset")
}
这在另一个类上抛出错误(lldb)不起作用:线程1:EXC_BREAKPOINT(代码= 1,子代码= 0x100318b4c)。此外,它不会打印 Assets ,所以我不相信它的设置是正确的。
但是当我使用时:
class TrimFootageViewController: UIViewController {
var movieURL:URL?
override func viewWillAppear(_ animated: Bool) {
playerView.playerLayer.player = player
super.viewWillAppear(animated)
self.thumbnailImage = setThumbnailFrom(path: movieURL!)
print(type(of: self.movieURL!))
guard let movieURL = URL(string: "https://devimages-cdn.apple.com/samplecode/avfoundationMedia/AVFoundationQueuePlayer_HLS2/master.m3u8") else {
return
}
asset = AVURLAsset(url: movieURL, options: nil)
print(asset ?? "couldn't get asset")
}
它可以正常工作并正确打印 <AVURLAsset: 0x101b00210, URL = https://devimages-cdn.apple.com/samplecode/avfoundationMedia/AVFoundationQueuePlayer_HLS2/master.m3u8>
self.movieURL!和 movieURL 在打印时都具有相同类型的 URL。另请注意,我像上一个 Controller 的 segue 中一样设置 self.movieURL:
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "TrimFootage_Segue" {
let controller = segue.destination as! TrimFootageViewController
controller.movieURL = self.videoRecorded
}
}
如何在 AVURLAsset 调用中正确设置 movieURL 资源,以便可以实例化它?
最佳答案
通过查看您的代码,似乎 movieURL
是 filePath,因为 setThumbnailFrom(path: movieURL!)
工作正常。也许这就是原因。
您可以通过应用 if-let
检查来避免崩溃:
class TrimFootageViewController: UIViewController {
var movieURL: URL?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
playerView.playerLayer.player = player
// Just check whether self.movieURL is filePath or URL
// For "setThumbnailFrom" passing as file path
// For "AVURLAsset(url: movURL, options: nil)" passing as URL
self.thumbnailImage = setThumbnailFrom(path: self.movieURL!) // Passing as filePath
if let movURL = self.movieURL as? URL, let asset = AVURLAsset(url: movURL, options: nil) {
print(asset)
} else {
print("Not able to load asset")
}
}
}
确保您从上一屏幕发送URL
:
let controller = segue.destination as! TrimFootageViewController
controller.movieURL = self.videoRecorded
关于ios - 如何将 URL 读入 AVURLAsset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49742307/