创建AVPlayerLayer会导致模拟器崩溃

时间:2016-11-04 07:30:32

标签: ios objective-c avplayer simulator

我有一个类在回调中返回AVPlayer和AVSynchronisedLayer。当我使用AVPlayer初始化AVPlayerLayer时,我遇到了崩溃(仅在模拟器中发生)。代码如下所示:

  self.projectPlayer = [[MYMobilePlayer alloc] init];
  [self.projectPlayer setDelegate:self]; 
  [self.projectPlayer returnPlayerForProjectScene:self.projectScene andCallback:^(NSError *error, AVPlayer *player, AVSynchronizedLayer *syncLayer) {


      self.syncLayer = syncLayer;
      AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
      NSLog(@"%@",playerLayer); // Also crashes if I assign to private member variable
      // More code that uses the playerLayer - not needed to illustrate the problem

  }];

回调中返回的对象是正确的,所以我认为我不需要显示创建并返回它们的代码。

堆栈跟踪如下所示(不是很有用)。

enter image description here

知道这里可能会发生什么吗?

1 个答案:

答案 0 :(得分:0)

应始终在主线程上调用

UIKit代码。您的returnPlayerForProjectScene回调发生在其他视频处理线程上。将有问题的代码分派到主队列应该可以解决崩溃问题:

[self.projectPlayer returnPlayerForProjectScene:self.projectScene andCallback:^(NSError *error, AVPlayer *player, AVSynchronizedLayer *syncLayer) {

    dispatch_async(dispatch_get_main_queue(), ^{

        self.syncLayer = syncLayer;
        AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
        NSLog(@"%@",playerLayer); // Also crashes if I assign to private member variable
        // More code that uses the playerLayer - not needed to illustrate the problem

    });
}];