I would like to animate the alpha of an png image in a UIImageView
based on the peakPowerForChannel of AVAudioPlayer
. I haven't got any errors or crashes, but its not working. Here are (what I believe) the relevant parts of my ViewController code:
.h
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property(getter = isMeteringEnabled) BOOL meteringEnabled;
@property (weak, nonatomic) IBOutlet UIImageView *mouthMeter;
- (void)updateMouthMeter:(id) sender;
.m
- (void)viewDidLoad{
...
self.meterTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateMouthMeter:) userInfo:NULL repeats:YES];
}
-(void)updateMouthMeter : (id)sender {
[self.audioPlayer updateMeters];
float level = [self.audioPlayer peakPowerForChannel:0];
[UIView beginAnimations:nil context:NULL];
self.mouthMeter.alpha = level;
[UIView commitAnimations];
}
I think my problem lies in the fact the AVAudioPlayer
outputs a MAX of 0.0dB and a MIN of -160dB, and I need to process this input so that it outputs a float (0.0 to 1.0) suitable for the alpha channel.
Any suggestions on how I can do this? Can it be included in the above updateMouthMeter method?