无法用声音播放AVMutableComposition

时间:2013-01-24 15:37:06

标签: audio video avplayer avmutablecomposition

我目前正在开发一款iPhone应用程序,只能播放.mp4的音轨。播放器开始播放,但我听不到任何声音。

以下是代码:

    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];

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,发现AVFoundation对你的作文格式非常不宽容。我最终回答了我自己的问题 - 这与你的问题相同。

查看我的帖子: iOS AVFoundation Export Session is missing audio