AVQueuePlayer (or AVPlayer) with a Soundcloud stream item block the UI (Main Thread)
Asked Answered
E

0

6

Thank to those two posts, I resolve this problem during the creation of the 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];

         });

     }];
});

But, with a stream from Soundcloud (https://api.soundcloud.com/tracks/146924238/stream?client_id=76395c48f97de903ff44861e230116bd), after the item's status is ready to play (AVPlayerStatusReadyToPlay), the AVQueueplayer keep doing something in the main thread "freezing" the UI (Track keep playing in background thread).

I noticed this problem is more important when the network is low, Then I made the conclusion that this freeze exist when the item is buffering the stream.

Someone have the solution or a trick?

Em answered 18/7, 2014 at 22:15 Comment(1)
In the Apple documentation using the asset init with options AVURLAssetPreferPreciseDurationAndTimingKey specifically states that this costs more because you are looking for precise duration. "YES indicates that longer loading times are acceptable in cases in which precise timing is required." Maybe this is your issue? Also, AVPlayerStatusReadyToPlay doesn't guarantee that the asset is actually ready to play. You may want to check AVPlayerItemStatus instead.Endocentric

© 2022 - 2024 — McMap. All rights reserved.