只是想了解,我可以用这个 AVMutableAudioMix 类做什么,一旦我将 PlayerItems(资产)插入到 AudioMix 中,我可以一次播放它们还是使用 AVPlayer 同时播放其中一些并更改参数动态的?
1 回答
我认为您可能会错误地对其进行可视化。AVMutableAudioMix 实例实际上是 AVPlayerItem 类的一个属性。首先使用 trackWithMediaType: 获取资源的轨道,然后使用audioMixInputParametersWithTrack :创建一个AVMutableAudioMixInputParameters实例 。在该 inputparameters 实例上设置任何音频属性(例如 setVolume:atTime)。
然后您需要将输入参数添加到 AVMutableAudioMix 实例中。然后您需要将其添加到播放器项目中。我知道这听起来令人困惑,但这正是 AVFoundation 与几乎所有东西一起工作的方式。到处都有术语,但几乎所有东西都有一个层次结构。
所以一般的层次结构是这样的:player->playerItem->audioMix->inputParameters。将音量从 5 秒降低到 7 秒的代码应如下所示:
AVAssetTrack *audioTrack = [[self.player.currentItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableAudioMixInputParameters *params = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTrack];
[params setVolumeRampFromStartVolume:1.0 toEndVolume:0.5 timeRange:CMTimeRangeMake(CMTimeMake(5,1), CMTimeMake(2,1))];
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = [NSArray arrayWithObject:params];
self.player.currentItem.audioMix = audioMix;
就动态执行此操作而言,您可以,但只能使用本地文件(而不是从互联网流式传输)。我可能会尝试将此 audioMix 保留为 ivar,并在每次您希望发生某些事情时尝试重置参数。如果这不起作用,您可能必须每次都创建一个 AVMutableAudioMix 实例,不确定。