简体   繁体   中英

Creating an AVPlayerLayer causes simulator to crash

I've got a class that returns an AVPlayer and AVSynchronisedLayer in a callback. When I use the AVPlayer to initialise an AVPlayerLayer, I get a crash (only happens in the simulator). The code looks like this:

  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

  }];

The objects being returned in the callback are correct, so I don't think I need to show the code that creates and returns them.

The stack trace is shown below (not very helpful).

在此输入图像描述

Any idea what might be happening here?

UIKit code should always be called on the main thread. Your returnPlayerForProjectScene callback is happening on some other video processing thread. Dispatching the code in question to the main queue should fix your crash:

[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

    });
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM