til*_*tem 6 iphone ios avaudiosession avplayer avurlasset
我有一个带有多个AVAssetTracks类型音频的AVURLAsset.我希望能够通过触摸按钮让用户在这些不同的音轨之间切换.它正在打开和关闭第一个音轨的音量,但是当音量设置为1.0时,听不到其他音轨.
这是用于调整音轨音量的代码(发送者是标签设置为audioTracks中资产索引的UIButton).
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];
Run Code Online (Sandbox Code Playgroud)
我是否需要做一些事情才能使所需的曲目成为活跃曲目?
好的,发现问题了。与上面的代码无关,因为这工作正常。问题是除第一个轨道之外的音频 AVAssetTracks 未启用。要启用它们,必须使用 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2944 次 |
| 最近记录: |