【问题标题】:Cocoa: AVAsset from raw data (i.e. NSData)Cocoa:来自原始数据的 AVAsset(即 NSData)
【发布时间】:2012-09-30 07:38:04
【问题描述】:

我想使用 AVFoundation 在 osx 上显示视频。我想在运行时从原始数据初始化电影。根据此文档:https://developer.apple.com/library/mac/#technotes/tn2300/_index.htmlAVAsset 相当于 QTKit 的 QTMovieQTMovie 具有 movieWithData:error: 从数据加载视频的功能,而我在 AVAsset 中找不到类似的东西。那么,是否有可能在 AVFoundation 中做同样的事情?

谢谢

【问题讨论】:

    标签: cocoa avfoundation qtkit


    【解决方案1】:
    NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"tmp.mp3"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createFileAtPath:tempFilePath contents:mp3Data attributes:nil];
    
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:tempFilePath] options:nil];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [manager removeItemAtPath:tempFilePath error:nil];
            });
    

    【讨论】:

    • 这没有回答问题,它只是展示了如何将NSData 写入一个临时文件并从中初始化一个AVAsset
    【解决方案2】:

    恐怕从原始数据初始化AVAsset 实例的唯一可能性是将之前的数据保存为文件。 AVAsset 及其子类仅在其构造函数中使用 URL。

    【讨论】:

      【解决方案3】:

      可以通过AVAssetResourceLoaderDelegate 实现从NSData 创建AVAsset

      具体实现:

      func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool
      

      必须:

      1. 正确填写loadingRequest.contentInformationRequest。提示,使用loadingRequest.contentInformationRequest.contentType = AVFileType.mycontenttype.rawValue
      2. 正确响应数据

      【讨论】:

      • 怎么做?我怎样才能找到你的示例代码?谢谢。
      猜你喜欢
      • 2012-01-18
      • 2013-09-28
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      相关资源
      最近更新 更多