Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAsset and AVAssetTrack - Track Management in IOS 4.0

Tags:

iphone

ios4

The new features list of IOS 4.0 says that AV Foundation framework has got Media asset management, Track management, Media editing, and Metadata management for media items. What do they mean by this?

  1. Using track management and media asset management can i access media files from the photos app?
  2. Can i make my custom compositions using AVComposition and export and send it to a server?
  3. Can i rename, move, edit the metadata information of an asset?

I tried to get some help/documentation on this and couldn't find any thing..

Thanks,

Tony

like image 450
Tony Avatar asked Jul 21 '10 10:07

Tony


1 Answers

Yes, you can do most of the stuff you mentioned. I think it's not that simple to access your media files of your phone. But you can read data from the network and export it to your camera roll if you like.

First you have to import your videos or audio files.

What you need to get started is your own videoplayer which you create in your own view. If you don't like to play your videos but simply compose your stuff, you can simply go without a view.

This is very easy: 1. create a mutable composition:

AVMutableComposition *composition = [AVMutableComposition composition];

This will hold your videos. Now you have an empty Composition-Asset. Add some files from your directory or from the web:

NSURL* sourceMovieURL = [NSURL fileURLWithPath:moviePath];
AVURLAsset* sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];

Then add your video to your composition

// calculate time
CMTimeRange editRange = CMTimeRangeMake(CMTimeMake(0, 600), CMTimeMake(sourceAsset.duration.value, sourceAsset.duration.timescale));

// and add into your composition 
BOOL result = [composition insertTimeRange:editRange ofAsset:sourceAsset atTime:composition.duration error:&editError];

If you would like to add more videos into your composition, you can add another Assets and set it again into your composition using your time range. Now you can EXPORT your new composition using code like this:

NSError *exportError = nil;

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];

NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = @"com.apple.quicktime-movie";
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    switch (exportSession.status) {
        case AVAssetExportSessionStatusFailed:{
            NSLog (@"FAIL");
            [self performSelectorOnMainThread:@selector (doPostExportFailed:)
                                        withObject:nil
                                                    waitUntilDone:NO];
            break;
        }
        case AVAssetExportSessionStatusCompleted: {
            NSLog (@"SUCCESS");
            [self performSelectorOnMainThread:@selector (doPostExportSuccess:)
                        withObject:nil
                        waitUntilDone:NO];
            break;
        }
    };
}];

If you want to PLAY your videos, use code like this (I assume, you have access to your view):

// create an AVPlayer with your composition
AVPlayer* mp = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]];

// Add the player to your UserInterface
// Create a PlayerLayer:
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:mp];

// integrate it to your view. Here you can customize your player (Fullscreen, or a small preview)
[[self view].layer insertSublayer:playerLayer atIndex:0];
playerLayer.frame = [self view].layer.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;  

And finally play your video:

[mp play];

Export to camera roll:

NSString* exportVideoPath = >>your local path to your exported file<<
UISaveVideoAtPathToSavedPhotosAlbum (exportVideoPath, self, @selector(video:didFinishSavingWithError: contextInfo:), nil);

And get the notification if it's finished (your callback method)

- (void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
    // Error is nil, if succeeded
    NSLog(@"Finished saving video with error: %@", error);
    // do postprocessing here, i.e. notifications or UI stuff

}

Unfortunately I haven't found any "legal" solution to read from the cameraroll.

A very good source on getting started is:

http://www.subfurther.com/blog/?cat=51

download VTM_Player.zip, VTM_AVRecPlay.zip or VTM_AVEditor.zip for a very nice introduction into this

like image 55
JackPearse Avatar answered Oct 10 '22 23:10

JackPearse