Mark
Mark

Reputation: 21

Can't play AVMutableComposition with sound

I'm currently working on an iPhone App, that only plays the Audio Track of an .mp4. The Player starts playing, but I can't hear any sound.

Here is the Code:

    NSURL *videoUrl = [NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];

    AVMutableComposition* composition = [AVMutableComposition composition];

    AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
    AVAssetTrack *audioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];

    NSError *error = nil;
    BOOL success = [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:audioTrack atTime:kCMTimeZero error:&error];

    if (!success)
    {
        NSLog(@"error: %@", error);
    }

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];

    self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

    [self.player play];

Upvotes: 2

Views: 2301

Answers (1)

ajs35
ajs35

Reputation: 448

I had a similar problem and just found that AVFoundation is very unforgiving in how you format your composition. I eventually answered my own question - which was the same as your's here.

Check out my post: iOS AVFoundation Export Session is missing audio

Upvotes: 2

Related Questions