我正在尝试创建一个类似于这样的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 (code=1,subcode=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
}
}如何在movieURL调用中正确设置AVURLAsset资产,使其能够被实例化?
发布于 2018-04-17 12:37:39
通过查看您的代码,似乎movieURL是filePath,因为setThumbnailFrom(path: movieURL!)运行良好。也许这就是原因。
通过应用if-let 检查as:,可以避免崩溃。
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发布于 2018-04-14 06:41:27
TrimFootageViewController中,定义一个var movieURLString = ""。movieURLString而不是movieURL。movieURL。也许没问题。
发布于 2018-04-17 08:45:38
我更新了你的密码。请看一下。它将不再崩溃,也请检查您是否正在从前一个控制器发送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")
}
}https://stackoverflow.com/questions/49742307
复制相似问题