Dan*_*avo 5 iphone objective-c ios swift avaudioengine
我有一个AVAudioEngine设置和一个正在播放一些背景音乐的AVAudioPlayerNode。
我正在尝试找到一种最佳方法,以在2秒的时间内在节点上创建音量渐弱。我正在考虑使用CADisplayLink来执行此操作。我想知道是否有人在这种情况下有经验,可以在方法上给我建议吗?
小智 5
我的方法如下。请注意,我指定计时器成员变种,所以我可以在其他点使它无效(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];
}
}
Run Code Online (Sandbox Code Playgroud)
小智 2
您可以在 EQ 中使用全局增益。
例如
AVAudioUnitEQ *Volume;
Volume = [[AVAudioUnitEQ alloc] init];
[engine attachNode:Volume];
[engine connect:Volume to:engine.outputNode format:nil];
Run Code Online (Sandbox Code Playgroud)
进而
Volume.globalGain = /*here your floatValue*/
Run Code Online (Sandbox Code Playgroud)