【问题标题】:MediaPlayer -requestThumbnailImagesAtTimes not firing a notificationMediaPlayer -requestThumbnailImagesAtTimes 未触发通知
【发布时间】:2014-09-23 17:08:18
【问题描述】:

我在使用 MPMoviePlayerController 从电影中提取缩略图时遇到问题

-requestThumbnailImagesAtTimes: timeOption:

我 99% 确定我已正确设置所有内容;我根本没有收到这些通知。

我原来在ReactiveCocoa工作;为了缩小可能性,我有一个没有它的最小损坏示例。

最小损坏示例:

@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];
  // ...
}

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!?");
  }];
}

【问题讨论】:

    标签: ios objective-c ios7 mpmediaplayercontroller


    【解决方案1】:

    您可能只需要稍微不同(和更早)设置通知。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(mySelector)
                                                     name:MPMoviePlayerThumbnailImageRequestDidFinishNotification
                                                   object:self.player];
    }
    
    -(void)mySelector {
        NSLog(@"Booya");
    }
    

    【讨论】:

      【解决方案2】:

      最初,(在 thumbnailImageAtTime: deprecated 之后)我也遇到了让requestThumbnailImagesAtTimes: timeOption: 正确触发的问题,尽管我发现从视频创建缩略图的有效方法是通过 AVAssetImageGenerator。从那以后我就一直使用这种方式,如果有帮助,请随意使用它作为替代方法。

      请参阅我之前回答的链接以及如何使用它。

      thumbnailImageAtTime: now deprecated - What's the alternative?

      【讨论】:

        【解决方案3】:
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(didReceiveImage:)
                                                     name:MPMoviePlayerThumbnailImageRequestDidFinishNotification
                                                   object:self.player];
        

        查看通知的对象。 您需要将其设置为 MPMoviePlayerController。 所以必须写在MPMoviePlayerController的首字母之后。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-30
          • 2018-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多