我有一个带AVAudioPlayerNode的AVAudioEngine设置,可以播放一些背景音乐。
我正在尝试找到一个最好的方法,在2秒的时间范围内在节点上创建一个体积淡出。我正在考虑使用CADisplayLink来做这件事。我想知道是否有人在这种情况下有经验,可以建议我他们的方法?
发布于 2016-07-01 21:53:58
我的方法如下。请注意,我将计时器分配给成员var,以便可以在其他点(viewWillDisappear、delloc等)使其无效。我担心它听起来不流畅,但我试过了,它工作得很好,不需要使用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];
}
}发布于 2016-01-13 18:58:11
你可以在EQ中使用全局增益。
例如
AVAudioUnitEQ *Volume;
Volume = [[AVAudioUnitEQ alloc] init];
[engine attachNode:Volume];
[engine connect:Volume to:engine.outputNode format:nil];然后
Volume.globalGain = /*here your floatValue*/发布于 2021-03-17 19:10:14
以防像我这样的人还在寻找答案:
中
类型别名完成= (() ->空) let mixer = AVAudioMixerNode() func fade(from: Float,to: Float,duration: TimeInterval,完成:完成?){设stepTime =0.01let times = duration / stepTime let step = (to - from) / Float(times) for i in 0...Int(times) { DispatchQueue.main.asyncAfter(deadline:.now() + Double(i) * stepTime) { mixer.volume = from + Float(i) * step if I == Int(次){完成?()}函数fadeIn(时长: TimeInterval = 1.3,完成:完成?=零){淡入淡出(从: 0,到: 1,持续时间,完成:完成)}fadeOut(持续时间: TimeInterval = 1.3,完成:完成?=零){淡入淡出(从: 1,到: 0,持续时间:持续时间,完成完成) }
https://stackoverflow.com/questions/32990266
复制相似问题