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

Но ничего не добавляется. Спасибо за вашу помощь (и извините за мой английский :))


person mat250    schedule 18.07.2011    source источник
comment
У вас получилось? если да то как?   -  person Suhas Aithal    schedule 10.09.2019


Ответы (1)


Я думаю, что эта строка:

NSURL *audioURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",URL_MEDIA,[[allAudioTracks objectAtIndex:i]аудио]]];

должен быть fileURLWithString вместо URLWithString, если вы используете файл. Общая ошибка. Дай мне знать.

person Vinnie    schedule 24.07.2011