gpt4 book ai didi

ios - 以编程方式淡出 AVAudioPlayerNode 的音量

转载 作者:搜寻专家 更新时间:2023-10-31 08:33:15 40 4
gpt4 key购买 nike

我有一个 AVAudioEngine 设置和一个正在播放一些背景音乐的 AVAudioPlayerNode。

我正在尝试找到一种最佳方法来在 2 秒的时间范围内在节点上创建音量淡出。我正在考虑使用 CADisplayLink 来做到这一点。我想知道是否有人有过这种情况的经验并且可以就他们的方法向我提出建议?

最佳答案

我的方法如下。请注意,我将计时器分配给一个成员 var,以便我可以在其他点(viewWillDisappeardelloc 等)使它无效。我担心它听起来不流畅,但我试了一下,它工作正常,不需要使用 CADisplayLink

- (void)fadeOutAudioWithDuration:(double)duration {
double timerInterval = 0.1;
NSNumber *volumeInterval = [NSNumber numberWithDouble:(timerInterval / duration)];
self.fadeOutTimer = [NSTimer scheduledTimerWithTimeInterval:timerInterval target:self selector:@selector(fadeOutTimerDidFire:) userInfo:volumeInterval repeats:YES];
}

- (void)fadeOutTimerDidFire:(NSTimer *)timer {
float volumeInterval = ((NSNumber *)timer.userInfo).floatValue;
float currentVolume = self.audioEngine.mainMixerNode.outputVolume;
float newValue = MAX(currentVolume - volumeInterval, 0.0f);
self.audioEngine.mainMixerNode.outputVolume = newValue;
if (newValue == 0.0f) {
[timer invalidate];
}
}

关于ios - 以编程方式淡出 AVAudioPlayerNode 的音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32990266/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com