0

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];
}
Prashant Nikam
  • 2,253
  • 4
  • 17
  • 29
Splosion Lowbeam
  • 73
  • 1
  • 1
  • 10
  • 1
    don't use animations for this. Simply set `self.mouthMeter.alpha` to your `alphaLevel`. The animations might be responsible for the flickering because it may take more time to run the animation than one of your 0.12 seconds update-cycles. Also, did you check the output of alphaLevel? Are there any values that reflect what you see? (alpha 0.0) – Swissdude Jul 08 '13 at 13:02
  • @swissdude I removed the animations-- same effect. With the NSTimer set to 0.12,the log shows that the alpha channel blinks to exactly 0.750000 every 25 updates. In between the blinks are different values in the 0.01-0.16 range, pretty low...hm! the low levels could just be the soundfile, it's a bit quiet. I will try another. – Splosion Lowbeam Jul 08 '13 at 20:43
  • Sorry, long day. Did you have any success with a different soundfile? Did you try to use channel 1 instead of channel 2? (`[self.audioPlayer averagePowerForChannel:1]`) Sorry, fishing a bit in the dark here... – Swissdude Jul 09 '13 at 21:04

0 Answers0