首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将URL读取到AVURLAsset中

如何将URL读取到AVURLAsset中
EN

Stack Overflow用户
提问于 2018-04-09 21:59:37
回答 3查看 1.1K关注 0票数 2

我正在尝试创建一个类似于这样的AVURLAsset:

代码语言:javascript
复制
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)。此外,它不打印资产,所以我不相信它是正确的。

然而,当我使用:

代码语言:javascript
复制
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的:

代码语言:javascript
复制
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资产,使其能够被实例化?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-04-17 12:37:39

通过查看您的代码,似乎movieURL是filePath,因为setThumbnailFrom(path: movieURL!)运行良好。也许这就是原因。

通过应用if-let 检查as:,可以避免崩溃。

代码语言:javascript
复制
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

代码语言:javascript
复制
let controller = segue.destination as! TrimFootageViewController
controller.movieURL = self.videoRecorded
票数 2
EN

Stack Overflow用户

发布于 2018-04-14 06:41:27

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

也许没问题。

票数 1
EN

Stack Overflow用户

发布于 2018-04-17 08:45:38

我更新了你的密码。请看一下。它将不再崩溃,也请检查您是否正在从前一个控制器发送URL(不能为零):

代码语言:javascript
复制
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")
        }    
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49742307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档