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];
}