Cocoa: AVAsset from raw data (i.e. NSData)

I would like to use AVFoundation to display video in osx. I would like to initialize a movie from raw data at runtime. According to this document: https://developer.apple.com/library/mac/#technotes/tn2300/_index.html AVAsset is the equivalent of QTKit QTMovie . QTMovie has a movieWithData:error: function for loading video from data, while I could not find anything like it in AVAsset . So, is it possible to do the same in AVFoundation at all ?

thanks

+8
source share
3 answers
 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]; }); 
+2
source

I am afraid that the unique opportunity to initialize an instance of AVAsset from raw data is to save the data earlier as a file. AVAsset and its subclasses use URLs only in their constructors.

+1
source

You can create AVAsset from NSData through the implementation of AVAssetResourceLoaderDelegate .

Specially implement:

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

It must:

  1. fill in loadingRequest.contentInformationRequest correctly. Hint, use loadingRequest.contentInformationRequest.contentType = AVFileType.mycontenttype.rawValue
  2. answer with the data correctly
0
source

All Articles