NSTimer creating a pulse effect in AVAudioPlayer and UIAnimation

落爺英雄遲暮 提交于 2019-12-13 06:03:48

问题


I've made a sound app with a UIVIew .png image (mouthMeter) whose alpha I would like to dim according to the averagePowerForChannel coming out of AVAudioPlayer. The alpha is being changed via updates from an NSTimer, meterTimer, 12 times a second (to simulate 12 fps). I also have a second NSTimer, sliderTimer which is updating the position of a UISlider once every second.

The dimming seems to be occuring, however there is a second effect of the alpha pulsing to fully-on (1.0). I can change the tempo of the pulses when I mess with the intervals of the NSTimers, and so I think they are interfering somehow, but I don't know how or why.

Any ideas? Anyone else have experience using audio from AVAudioPlayer to drive and UIAnimation?

h.file

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property(getter = isMeteringEnabled) BOOL meteringEnabled;
@property (nonatomic, strong) NSTimer *sliderTimer;
@property(nonatomic,strong) NSTimer * meterTimer;
...

m.file

- (void)viewDidLoad{
...
self.sliderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     
selector:@selector(updateSlider:) userInfo:NULL repeats:YES];
self.meterTimer = [NSTimer scheduledTimerWithTimeInterval:0.12 target:self   
selector:@selector(updateMouthMeter:) userInfo:NULL repeats:YES];
...
}

-(void)updateMouthMeter : (id)sender {
[self.audioPlayer updateMeters];
float level = [self.audioPlayer averagePowerForChannel:0];
float alphaLevel = (((level * -1.0)/160)+0.3);
[UIView beginAnimations:nil context:NULL];
self.mouthMeter.alpha = alphaLevel;
[UIView commitAnimations];
}

来源:https://stackoverflow.com/questions/17526084/nstimer-creating-a-pulse-effect-in-avaudioplayer-and-uianimation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!