使用多个音频类型的 AVAssetTrack 切换 AVURLAsset 的音频轨迹
我有一个 AVURLAsset 和多个音频类型的 AVAssetTrack。我希望能够允许用户通过触摸按钮在这些不同的音轨之间切换。它可以打开和关闭第一首曲目的音量,但当音量设置为 1.0 时听不到其他曲目。
以下是用于调整轨道音量的代码(发送者是一个 UIButton,其标签设置为 audioTracks 中资产的索引)。
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];
我需要做些什么才能使所需的曲目成为活动曲目吗?
I have an AVURLAsset with multiple AVAssetTracks of type audio. I would like to be able to allow user to switch between these different audio track by touching a button. It is working to turn volume of 1st track on and off but other tracks are not heard when volume set to 1.0.
Here is code for adjusting the volume of the tracks (sender is a UIButton with tag set to index of asset in audioTracks).
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];
Do I need to do something to bring the desired track to be the active one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,发现问题了。与上面的代码无关,因为这工作正常。问题是除第一个轨道之外的音频 AVAssetTracks 未启用。要启用它们,必须使用 AVMutableComposition 重新创建资产:
Ok found issue. Was not related to above code as this works fine. The problem was the AVAssetTracks for audio other than 1st track were not enabled. To enable them had to recreate the asset using AVMutableComposition: