如何将 URL 读入 AVURL 资产

How to read a URL into an AVURLAsset

我正在尝试像这样创建一个 AVURL资产:

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")

    }

在另一个 class 上抛出错误 (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 调用中正确设置电影URL asset 以便它可以被实例化?

  1. TrimFootageViewController中,定义一个var movieURLString = "".
  2. 在前一个控制器的segue中: 设置 movieURLString 而不是 movieURL.
  3. 然后,使用你的第二种方式初始化movieURL

也许可以。

I have updated your code. Please have a look. It will not crash anymore and also please check that you are sending the URL(it cannot be nil) from the previous controller:

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")
        }    
    }

通过查看您的代码,movieURL 似乎是文件路径,因为 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