感谢这两篇文章,我在创建AVPLayerItem期间解决了这个问题 AVQueuePlayer playback without gap and freeze AVPlayer "freezes" the app at the start of buffering an audio stream
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:urlString] options:@{AVURLAssetPreferPreciseDurationAndTimingKey : @(YES)}];
NSArray *keys = @[@"playable", @"tracks",@"duration" ];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^()
{
for (NSString *thisKey in keys) {
NSError *error = nil;
AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
if (keyStatus == AVKeyValueStatusFailed) {
return ;
}
}
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
dispatch_async(dispatch_get_main_queue(), ^ {
[item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
[player insertItem:item afterItem:nil];
});
}];
});
但是,使用来自Soundcloud(https://api.soundcloud.com/tracks/146924238/stream?client_id=76395c48f97de903ff44861e230116bd)的流,在项目状态准备好播放(AVPlayerStatusReadyToPlay)之后,AVQueueplayer继续在主线程中执行某些操作"冻结&#34 ; UI(跟踪继续在后台线程中播放)。
我注意到这个问题在网络较低时更为重要,然后我得出结论,当项目缓冲流时,此冻结存在。
有人有解决方案或技巧吗?