我有一个类在回调中返回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
}];
回调中返回的对象是正确的,所以我认为我不需要显示创建并返回它们的代码。
堆栈跟踪如下所示(不是很有用)。
知道这里可能会发生什么吗?
答案 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
});
}];