使用多个音频类型的 AVAssetTrack 切换 AVURLAsset 的音频轨迹

发布于 2024-12-04 03:01:19 字数 1011 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港巷口少年归 2024-12-11 03:01:19

好的,发现问题了。与上面的代码无关,因为这工作正常。问题是除第一个轨道之外的音频 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);

}

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:

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文