2

I’m having trouble extracting thumbnail images out of movies with MPMoviePlayerController

-requestThumbnailImagesAtTimes: timeOption:

I'm 99% sure I've set everything up correctly; I'm just not getting those notifications at all.

I was originally working in ReactiveCocoa; to narrow down the possibilities I've got a minimal broken example without it though.

Minimal broken example:

@import MediaPlayer;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  // 1. register the observer before requesting the thumbnails
  [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
    // 4. this never gets hit
    NSLog(@"%@", note.name);
  }];

  // 2. this works fine - media url is correct etc
  MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[info objectForKey:UIImagePickerControllerMediaURL]];

  // 3. previously was using integers instead of floats; fixed that but this still doesn't do anything
  [moviePlayer requestThumbnailImagesAtTimes:@[ @0.0f, @1.0f ] timeOption:MPMovieTimeOptionExact];
  // ...
}

Original example in ReactiveCocoa

@import MediaPlayer;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  // 1. set the file URL
  self.viewModel.movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
  // ...
}

// in viewmodel

- (void)viewDidLoad {
  RACSignal *moviePlayerSignal = [[RACObserve(self, movieURL) ignore:nil] map:^id(NSURL *url) {
    // 2. this allocates correctly
    return [[MPMoviePlayerController alloc] initWithContentURL:url];
  }];

  // 3. observe the moviePlayerSignal; 
  [[[moviePlayerSignal map:^id(MPMoviePlayerController *player) {
    @strongify(self);
    NSLog(@"%@", player); // checking that the player exists etc - it does; all good here.

    // register the observer before we request the thumbnail
    RACSignal *notification = [[[NSNotificationCenter defaultCenter] rac_addObserverForName:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:player] takeUntil:[self rac_willDeallocSignal]];

    // request the thumbnail
    [player requestThumbnailImagesAtTimes:@[ @0.0f ] timeOption:MPMovieTimeOptionExact];

    // map the signal into a stream of signals on the observer
    return notification;

    // if we subscribeNext without flattening we correctly get back RACSignals every time 
  }] flatten] subscribeNext:^(id x) {
    // the flattened signal never gets a next because the player isn't firing notifications :(
    NSLog(@"WHY DOESN'T THIS WORK!?");
  }];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
Jon Gold
  • 1,173
  • 12
  • 17

3 Answers3

1
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didReceiveImage:)
                                             name:MPMoviePlayerThumbnailImageRequestDidFinishNotification
                                           object:self.player];

See the object of the notification. You need to set it as the MPMoviePlayerController. So it must be written after the initial of the MPMoviePlayerController.

KChen
  • 1,922
  • 14
  • 15
0

You might just need to set up your notification slightly differently (and earlier).

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(mySelector)
                                                 name:MPMoviePlayerThumbnailImageRequestDidFinishNotification
                                               object:self.player];
}

-(void)mySelector {
    NSLog(@"Booya");
}
0

Initially, (after thumbnailImageAtTime: deprecated) I also had issue getting requestThumbnailImagesAtTimes: timeOption: to fire properly, though I found an effective way to create thumbnails from the video is via AVAssetImageGenerator. I've used this way ever since, feel free to use it as an alternative if it helps.

Please see link to my previous answer with how to use it.

thumbnailImageAtTime: now deprecated - What's the alternative?

Community
  • 1
  • 1
Jim Tierney
  • 4,078
  • 3
  • 27
  • 48