1

I am trying to use AVURLAsset to load a webvtt file.

Below is my code.

    NSString *urlAddress = @"http://somewhere/some.vtt";
    NSURL *urlStream = [[NSURL alloc] initWithString:urlAddress];
    AVAsset *avAsset = [AVURLAsset URLAssetWithURL:urlStream options:nil];

    NSArray *requestKeys = [NSArray arrayWithObjects:@"tracks",@"playable",nil];
    [avAsset loadValuesAsynchronouslyForKeys:requestKeys completionHandler:^{
    dispatch_async(dispatch_get_main_queue(),^{

        //complete block here
        AVKeyValueStatus status =[avAsset statusOfValueForKey:@"tracks" error:nil];
        if(status == AVKeyValueStatusLoaded) {

            //loaded block !
            //Question 1
            CMTime assetTime = [avAsset duration];
            Float64 duration = CMTimeGetSeconds(assetTime);
            NSLog(@"%f", duration);

            //Question 2
            AVMediaSelectionGroup *subtitle = [avAsset mediaSelectionGroupForMediaCharacteristic: AVMediaCharacteristicLegible];
            NSLog(@"%@", subtitle);
        }
        else {
            //don’t load block ! 
        }
    });
}];

Question 1: It always go into the "Loaded Block", but I find the avAsset's duration is not complete, that means the data is not loaded? How should I modify it?

Question 2: I am trying to use it to my avplayer's subtitle, but the AVMediaSelectionGroup is always null. What should I do?

Pang
  • 9,564
  • 146
  • 81
  • 122
Leon Shiu
  • 13
  • 4

2 Answers2

0

For question 1, add duration to your keys:

NSArray *requestKeys = @[@"tracks",@"playable", @"duration"];
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
0

I've posted a solution over here: https://stackoverflow.com/a/37945178/171933 Basically you need to use an AVMutableComposition to join the video with the subtitles and then play back that composition.

About your second question: mediaSelectionGroupForMediaCharacteristic seems to only be supported when these "characteristics" are already baked into either the media file or your m3u8 stream, according to this statement by an Apple engineer (bottom of the page).

Community
  • 1
  • 1
Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165