我的要求:我需要从视频中获取所有音轨的列表,然后切换到选定的音轨。
方法和问题: 我使用两种方式,因为有些时候第一种方式不提供结果,在这种情况下我使用第二种方式,有时两种方法都没有得到结果,而VLC媒体播放器显示视频有音轨。
我看了苹果文档 https://developer.apple.com/library/mac/releasenotes/AudioVideo/RN-AVFoundation/index.html#//apple_ref/doc/uid/TP40010717-CH1-DontLinkElementID_1 这是第一种方式。
获取音轨的我的代码 - :
- (NSArray *)getAvailableAudioTracks
{
NSMutableArray *allAudioTracks = [NSMutableArray new];
AVURLAsset *asset = (AVURLAsset *)[[self.mPlayer currentItem] asset];
//** 1st Way
AVMediaSelectionGroup *audioTracks = [asset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
for (AVMediaSelectionOption *option in audioTracks.options)
{
NSLog(@"Audio Track Display Name: %@", option.displayName);
[allAudioTracks addObject:option];
}
//** 2nd way
NSArray *audiTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
if(allAudioTracks.count == 0) {
allAudioTracks = [NSMutableArray arrayWithArray:audiTracks];
}
[self.audioTracks removeAllObjects];
self.audioTracks = allAudioTracks;
return allAudioTracks;
}
切换到所选曲目的代码 -
- (void)playSelectedAudioTracks:(NSArray *)selectedAudioTracks {
if (selectedAudioTracks.count == 0 || self.audioTracks.count <2) {
return;
}
for (id audioTrack in selectedAudioTracks) {
if ([audioTrack isKindOfClass:[AVMediaSelectionOption class]]) {
[self changeAudioTrackWithSelectedAudioOption: audioTrack];
} else if ([audioTrack isKindOfClass:[AVAssetTrack class]]) {
AVAsset *asset = [[self.mPlayer currentItem] asset];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks)
{
float trackVolume = 0.0;
if ([selectedAudioTracks containsObject:track]) {
trackVolume = 1.0;
}
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:trackVolume atTime:kCMTimeZero];
[audioInputParams setTrackID:[track trackID]];
[allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
dispatch_async(dispatch_get_main_queue(), ^{
[[self.mPlayer currentItem] setAudioMix:audioZeroMix];
});
}
}
}
- (void)changeAudioTrackWithSelectedAudioOption:(AVMediaSelectionOption *)selectedAudioOption {
AVAsset *asset = [[self.mPlayer currentItem] asset];
AVMediaSelectionGroup *audoTracks = [asset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
[[self.mPlayer currentItem] selectMediaOption:selectedAudioOption inMediaSelectionGroup:audoTracks];
}
问题: Q1)为什么第一种方式不能工作最长时间?我究竟做错了什么?从视频中找出音轨的正确方法是什么?
Q2)我正在使用两种方法
a) - :使用第一种方式获取音频大头钉,然后使用changeAudioTrackWithSelectedAudioOption
:此方法。
b)使用2nd way获取音频曲目,然后使用AVMutableAudioMixInputParameters
类调整音量。
我不知道这两个有什么区别。请告诉我这两个有什么不同?我知道苹果建议第一种方式,但仍然想要两个知道第二种方法是否正确?
谢谢.... :)