Tuesday, February 8, 2011

AVURLAsset

NSURL *url = [[NSURL alloc] initWithString:@"http://movies.apple.com/media/us/mac/getamac/2009/apple-mvp-biohazard_suit-us-20090419_480x272.mov"];
   
   
    //NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]                                forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
   
   
    anAssest= [[AVURLAsset alloc]initWithURL:url options:nil];
    //NSLog(@"option= %@",options);
   
    NSString *tracksKey = @"tracks";
    [anAssest loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:^{
        NSError *error = nil;
        AVKeyValueStatus status = [anAssest statusOfValueForKey:tracksKey error:&error];
        if (status == AVKeyValueStatusLoaded) {
            self.playerItem = [AVPlayerItem playerItemWithAsset:anAssest];
            self.player = [AVPlayer playerWithPlayerItem:playerItem];
            //[playerItem addObserver:self forKeyPath:@"status"
                        //    options:0 context:&ItemStatusContext];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification      object:playerItem];
            [playerView setPlayer:player];
            [player play];
                       
        }
        else
        {
            NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
        }
    }];
    NSLog(@"anAssest.lyrics = %@",anAssest.lyrics );
    NSLog(@"anAssest.duration  = %f",anAssest.duration );
    NSLog(@"anAssest.naturalSize  = %d",anAssest.naturalSize);
    NSLog(@"anAssest.tracks  = %d",anAssest.tracks);
   
   
    NSArray *keys = [NSArray arrayWithObject:@"duration"];
   
    [anAssest loadValuesAsynchronouslyForKeys:keys completionHandler:^(void) {
        NSError *error = nil;
        AVKeyValueStatus durationStatus = [anAssest statusOfValueForKey:@"duration" error:&error];
       
        switch (durationStatus) {
            case AVKeyValueStatusLoaded:
                [self updateUserInterfaceForDuration];
                NSLog(@"anAssest.duration  = %f",anAssest.duration );
                NSLog(@"durationStatus = %d",durationStatus);
                break;
            case AVKeyValueStatusFailed:
                //[self reportError:error forAsset:anAssest];
                break;
            case AVKeyValueStatusCancelled:
                // Do whatever is appropriate for cancelation.
                break;
        }}];
   
}
-(IBAction)currenttime:(id)sender{
   
        //[player pause];
        NSLog(@"anAssest.duration  = %f",anAssest.duration );
        //CMTime fiveSecondsIn = CMTimeMake(5, 1);
        //[player seekToTime:fiveSecondsIn toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
        //i=1;
        [player play];
        AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:anAssest];
       
        Float64 durationSeconds = CMTimeGetSeconds([anAssest duration]);
        NSLog(@"durationSeconds %f",durationSeconds);
        CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/i, 600);
        NSError *error = nil; CMTime actualTime;
        CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
        if (halfWayImage != NULL) {
            NSString *actualTimeString = (NSString *)CMTimeCopyDescription(NULL, actualTime);
            NSString *requestedTimeString = (NSString *)CMTimeCopyDescription(NULL, midpoint);
            NSLog(@"got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);
            UIImage *img =[UIImage imageWithCGImage:halfWayImage];
            UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 350, 100, 50)];
            imgview.image=img;
            [self.view addSubview:imgview];
            [actualTimeString release];
            [requestedTimeString release];
            // Do something interesting with the image. CGImageRelease(halfWayImage);
        } [imageGenerator release];
       
               
    i++;
   
}   
-(void)updateUserInterfaceForDuration{
   
NSLog(@"anAssest.duration  = %f",anAssest.duration );


}

1 comment:

  1. what is this code for???
    don't You release the AVURLAsset???

    thanks!

    ReplyDelete