AVAsset and HLS Streaming

I try to play a m3u8 file and a mp3 file simultaneously. but it is not working.


    NSURL *audioURL = [NSURL URLWithString:@"https://***.mp3"];
    AVAsset *audioAsset = [AVAsset assetWithURL:audioURL];

    NSURL *videoURL = [NSURL URLWithString:@"https://***.m3u8"];
    AVAsset *videoAsset = [AVAsset assetWithURL:videoURL];

    NSError *error;

    AVMutableComposition* mixAsset = [[AVMutableComposition alloc] init];

    AVMutableCompositionTrack* audioTrack = [mixAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error: &error];
  
    AVMutableCompositionTrack* videoTrack = [mixAsset addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error: &error];
  
  
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:mixAsset];

    movie = [AVPlayer playerWithPlayerItem:playerItem];

i get the following error:


*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array


another mp4 video file is working but not with m3u8 file (hls streaming). How is the right way to work with such files in a AVMutableCompositionTrack?

Replies

Unfortunately, before using an AVMutableCompositionTrack you must download your HLS and cached locally.