2011-09-12 32 views
6

Tôi có AVURLAsset với nhiều AVAssetTracks loại âm thanh. Tôi muốn có thể cho phép người dùng chuyển đổi giữa các bản âm thanh khác nhau này bằng cách chạm vào nút. Nó đang làm việc để bật âm lượng của bài hát thứ nhất và tắt nhưng các bài hát khác không được nghe khi âm lượng được đặt thành 1.0.Chuyển đổi trakcs âm thanh cho AVURLNhập với nhiều AVAssetTracks loại âm thanh

Đây là mã để điều chỉnh âm lượng của bản nhạc (người gửi là UIButton có thẻ được đặt thành chỉ mục nội dung trong bản âm thanh).

AVURLAsset *asset = (AVURLAsset*)[[player currentItem] asset]; 
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio]; 


NSMutableArray *allAudioParams = [NSMutableArray array]; 
int i = 0; 
NSLog(@"%@", audioTracks); 
for (AVAssetTrack *track in audioTracks) { 
    AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters]; 
    float volume = i == sender.tag ? 1.0 : 0.0; 
    [audioInputParams setVolume:volume atTime:kCMTimeZero]; 
    [audioInputParams setTrackID:[track trackID]]; 
    [allAudioParams addObject:audioInputParams]; 
    i++; 
} 
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix]; 
[audioZeroMix setInputParameters:allAudioParams]; 

[[player currentItem] setAudioMix:audioZeroMix]; 

Tôi có cần làm điều gì đó để mang bản nhạc mong muốn trở thành hoạt động không?

Trả lời

4

Ok vấn đề đã tìm thấy. Không liên quan đến mã trên vì điều này hoạt động tốt. Sự cố là AVAssetTracks cho âm thanh không phải là bản nhạc thứ nhất đã không được bật. Để cho phép họ phải tạo lại nội dung bằng AVMutableComposition:

NSURL *fileURL = [[NSBundle mainBundle] 
        URLForResource:@"movie" withExtension:@"mp4"]; 

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil]; 

AVMutableComposition *composition = [AVMutableComposition composition]; 

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

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

NSArray *allAudio = [asset tracksWithMediaType:AVMediaTypeAudio]; 
for (int i=0; i < [allAudio count]; i++) { 
    NSError* error = NULL; 
    AVAssetTrack *audioAsset = (AVAssetTrack*)[allAudio objectAtIndex:i]; 

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

    NSLog(@"Error : %@", error); 

} 
+0

Cảm ơn rất nhiều! Điều này đã giúp rất nhiều! –

+0

Cảm ơn bạn, cảm ơn bạn, cảm ơn bạn! Điều này cực kỳ hữu ích! –