Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'd like to use AVFoundation to display a video on osx. I would like to initialize the 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's QTMovie. QTMovie has the function movieWithData:error: to load the video from data, while i could not find anything similar in AVAsset. So, is it possible to do the same thing in AVFoundation at all?

Thank you

like image 466
moka Avatar asked Sep 30 '12 07:09

moka


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];
        });
like image 113
Tuğrul Özdemir Avatar answered Oct 21 '22 05:10

Tuğrul Özdemir


I am afraid the unique possibility to initialize AVAsset instance from raw data is to save the data before as a file. AVAsset and its subclasses are using URLs only in their constructors.

like image 42
voromax Avatar answered Oct 21 '22 05:10

voromax


It is possible to create an AVAsset from an NSData via an AVAssetResourceLoaderDelegate implementation.

Specifically implement:

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

It must:

  1. fill in the loadingRequest.contentInformationRequest correctly. Hint, use loadingRequest.contentInformationRequest.contentType = AVFileType.mycontenttype.rawValue
  2. respond with data correctly
like image 32
iamacomputer Avatar answered Oct 21 '22 03:10

iamacomputer