AVPlayer具有一个视频轨道和多个音频轨道

3

我正在尝试让我的应用程序中的播放器可以拥有一个视频轨道和多个音频轨道(用于不同的语言)。

我已经做到了这一点,但是播放器无法启动:

AVMutableComposition *composition = [AVMutableComposition composition];
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:urlVideo options:nil];

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError* error = NULL;

[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration) 
                               ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0] 
                                atTime:kCMTimeZero
                                 error:&error];

NSMutableArray *allAudio = [[NSMutableArray alloc]init];
for (int i=1; i < [allAudioTracks count]; i++) {
    NSURL *audioURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_MEDIA,[[allAudioTracks objectAtIndex:i]audio]]];
    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil];
    [allAudio addObject:audioAsset];
    [audioAsset release];
    [audioURL release];
}

for (int i=0; i < [allAudio count]; i++) {
    NSError* error = NULL;
    AVURLAsset *audioAsset = (AVURLAsset*)[allAudio objectAtIndex:i];
    //audioAsset = [allAudio objectAtIndex:i];

    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,audioAsset.duration) 
                                    ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]
                                     atTime:kCMTimeZero
                                      error:&error];

    NSLog(@"Error : %@", error);
    //[allCompositionTrack addObject:compositionAudioTrack];
    [audioAsset release];
}

我尝试这样启动我的播放器:

AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithAsset:composition];

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

layerVideo = [AVPlayerLayer playerLayerWithPlayer:player];
layerVideo.frame = CGRectMake(0, 0, 480, 320);
[self.view.layer addSublayer:layerVideo];

[player play];

但没发生任何事情。感谢你的帮助(我的英语不好请见谅)。

你搞定了吗?如果是,怎么做到的? - Suhas Aithal
1个回答

3

我认为这行代码:

NSURL *audioURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_MEDIA,[[allAudioTracks objectAtIndex:i]audio]]];

如果你使用的是文件,那么必须使用fileURLWithString而不是URLWithString。这是一个常见的错误。让我知道吧。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接