8

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

moka
  • 4,353
  • 2
  • 37
  • 63

3 Answers3

2
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];
        });
  • 1
    This doesn't answer the question, it just shows how to write the `NSData` to a temporary file and initialize an `AVAsset` from that. – Cutterpillow Apr 13 '19 at 03:00
1

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.

voromax
  • 3,369
  • 2
  • 30
  • 53
1

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
iamacomputer
  • 518
  • 3
  • 14