2

I have been struggling with a problem where avplayer is not showing the video asset if I add audio to the composition.

Here is the code as it is now:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

    AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"http://xxx.xxx.xxx/xxx.mp4"] options:nil];
    AVURLAsset *audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"http://xxx.xxx.xxx/xxx.mp3"] options:nil];

    AVMutableCompositionTrack *cmpTrackVideo = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *cmpTrackAudio = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack *clipVideo = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVAssetTrack *clipAudio = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    [cmpTrackVideo insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:clipVideo atTime:kCMTimeZero error:nil];
    [cmpTrackAudio insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:clipAudio atTime:kCMTimeZero error:nil];

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];
    self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

    AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    [playerLayer setFrame:self.view.bounds];
    [self.view.layer addSublayer:playerLayer];

    [self.player play];
}

I have also tried the way with playerItem.videoComposition and avmutablevideocompositioninstruction etc... with no luck.

Does anyone have any clue about what could be causing this?

I would be happy with any inputs, tips, examples etc.

Best regards, Neno

Neno
  • 23
  • 2

0 Answers0