ios - 如何将 URL 读入 AVURLAsset

标签 ios swift avfoundation uistoryboardsegue

我正在尝试创建一个 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.movi​​eURL!和 movieURL 在打印时都具有相同类型的 URL。另请注意,我像上一个 Controller 的 segue 中一样设置 self.movi​​eURL:

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/

相关文章:

ios - 将 UIImage 分配给 Collection View 单元格

iOS Today 小部件在底部被截断

iphone - 将 AVAudioPlayer 音量设置为用户的铃声音量?

swift - 媒体信息显示在模拟器中但不显示在真实设备上

ios - captureStillImageAsynchronouslyFromConnection 的函数声明和参数描述冲突?

iOS SDK Mutual Friends Facebook API v2.0

ios - UILocalNotification 没有做任何事情

设备锁定时的 iOS 推送通知操作

ios - NSLayoutConstraint.constraintsWithVisualFormat 在 Swift 2.0 中的 options 参数中传递 nil

ios - 继承问题 - super 后预期为 '.' 或 '['