iphonefileavfoundationmovies

how to load multiple local files with the AVFoundation


I wish to use the AVFoundation to load and play movie files. The first problem I have is attempting to load multiple local files. The code I found only allows me to load one. If I call the method below it exits with the exception:

Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer for the key path "currentItem.nonForcedSubtitleDisplayEnabled" from because it is not registered as an observer.'

Thanks for any advice

- (IBAction)loadAssetFromFile:(NSString*)fileName withExtension:(NSString*)extension {

NSURL *fileURL = [[NSBundle mainBundle]
                  URLForResource:fileName withExtension:extension];

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *tracksKey = @"tracks";

[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
 ^{

     // Completion handler block.
     NSError *error = nil;
     AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];

     if (status == AVKeyValueStatusLoaded) {

         self.playerItem = [AVPlayerItem playerItemWithAsset:asset];

         [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];

         [[NSNotificationCenter defaultCenter] addObserver:self
                                                  selector:@selector(playerItemDidReachEnd:)
                                                      name:AVPlayerItemDidPlayToEndTimeNotification
                                                    object:playerItem];
         self.player = [AVPlayer playerWithPlayerItem:playerItem];
         [playerView setPlayer:player];
     }
     else {
         // Deal with the error appropriately.
         NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
     }

 }];

}


Solution

  • oops i figured it out myself, check out my answer to this question: MPMoviePlayerController switching movies causes white flash