具有一个视频轨道和多个音频轨道的 AVPlayer
我试图让我的应用程序中的播放器能够拥有一个视频轨道和多个音频轨道(针对不同的语言)。
我已经这样做了,但播放器不会启动:
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];
但没有任何附加内容。 感谢您的帮助(对我的英语感到抱歉:))
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 :))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这一行:
NSURL *audioURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_MEDIA,[[allAudioTracks objectAtIndex:i]audio]]];
如果您使用的是文件,则必须是 fileURLWithString 而不是 URLWithString。常见错误。让我知道。
I think this line:
NSURL *audioURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_MEDIA,[[allAudioTracks objectAtIndex:i]audio]]];
has to be fileURLWithString instead of URLWithString if you are using a file. Common mistake. Let me know.