我正在尝试创建一个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(。此外,它不会打印资产,因此我认为它设置不正确。
但是当我使用时:
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。另请注意,我在以前的控制器的 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
- 在
TrimFootageViewController
中,定义一个var movieURLString = ""
。 - 在上一个控制器的 segue 中:设置
movieURLString
而不是movieURL
。 - 然后,使用第二种方式初始化
movieURL
。
也许还可以。
我已经更新了你的代码。请看一看。它不会再崩溃了,还请检查您是否正在从以前的控制器发送URL(不能为零(:
class TrimFootageViewController: UIViewController {
var movieURL: URL?
override func viewWillAppear(_ animated: Bool) {
playerView.playerLayer.player = player
super.viewWillAppear(animated)
if let mURL = movieURL {
self.thumbnailImage = setThumbnailFrom(path: mURL)
print(type(of: mURL))
asset = AVURLAsset(url: mURL, options: nil)
print(asset ?? "couldn't get asset")
}
}