AVPlayer具有一个视频轨道和多个音频轨道 [英] AVPlayer with one video tracks and multiples audio tracks
问题描述
我正在尝试使我的App中的播放器能够具有一个视频轨道和多个音频轨道(针对不同的语言).
I'm trying to make a player in my App capable to have one video tracks and multiples audio tracks (for differents languages).
我已经做到了,但是播放器无法启动:
I've done this but the player won't launch :
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];
}
然后我尝试像这样启动我的播放器:
and I try to start my player like this :
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];
但是没有附加. 谢谢您的帮助(对不起我的英语:))
But nothing append. Thanks for your help (and sorry for my English :))
推荐答案
我认为这行:
NSURL * audioURL = [NSURL URLWithString:[NSString stringWithFormat:@%@%@",URL_MEDIA,[[allAudioTracks objectAtIndex:i] audio]]]]
NSURL *audioURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_MEDIA,[[allAudioTracks objectAtIndex:i]audio]]];
必须是fileURLWithString而不是URLWithString.常见的错误.让我知道.
has to be fileURLWithString instead of URLWithString if you are using a file. Common mistake. Let me know.
这篇关于AVPlayer具有一个视频轨道和多个音频轨道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!