3

I'm trying to get the natural size of the video track of an AVURLAsset. So I'm loading the asset tracks with loadValuesAsynchronouslyForKeys:completionHandler:. In the completion handler, when the status is AVKeyValueStatusLoaded I look at the asset.tracks and it is empty. I'm expecting to find at least an audio and a video track. Why is the tracks array empty?

Here is my code:

#import <AVFoundation/AVFoundation.h>

int main(int argc, char *argv[])
{
    __block BOOL loaded = NO;
    AVURLAsset *asset;
    @autoreleasepool
    {
        NSURL *url = [NSURL URLWithString:@"https://devimages.apple.com.edgekey.net/resources/http-streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8"];
        asset = [AVURLAsset URLAssetWithURL:url options:nil];
        [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
            loaded = YES;
            dispatch_async(dispatch_get_main_queue(), ^{
                NSError *error = nil;
                AVKeyValueStatus status = [asset statusOfValueForKey:@"tracks" error:&error];
                if (status == AVKeyValueStatusLoaded)
                    NSLog(@"asset.tracks (%ld): %@", [asset.tracks count], asset.tracks);
                else
                    NSLog(@"error (%ld): %@", status, error);
            });
        }];
    }

    while (!loaded)
        [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

    return 0;
}

Running this code on Mountain Lion 10.8.1 logs this:

asset.tracks (0): (
)

UPDATE Some people pointed to me that I may not get tracks because I'm using a HTTP Live Streaming URL. So any way (using tracks or not) to get the size of the currently playing video would be an acceptable answer.

1 Answer 1

5

Since the asset is served with HTTP Live Streaming it does not have any track. Another way of retrieving the video size is to observe the player item presentationSize property with KVO:

AVPlayer *player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:asset]];
[player addObserver:self forKeyPath:@"currentItem.presentationSize" options:NSKeyValueObservingOptionNew context:NULL];
3
  • @oxced: I want to get current bitrate of player. Is there anyway to do so with HLS? Jul 8, 2015 at 8:33
  • @0xced: Is this an OSX-only issue? I don't see a problem with this on iOS. Apr 13, 2016 at 19:02
  • I just tried that code on bot OS X 10.11.4 and iOS 9.3 and I get the same result: 0 tracks!
    – 0xced
    Apr 13, 2016 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.