0

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?

djf
  • 6,592
  • 6
  • 44
  • 62
Splosion Lowbeam
  • 73
  • 1
  • 1
  • 10
  • I might be wrong, but that's some simple math? value x = 1.0 / (160 - (MIN_DB * -1)) -> x equals the value for the alpha channel – Swissdude Jul 05 '13 at 20:36
  • @Swissdude ok! so because it's giving me the peak (MAX dB) I've got this going on: _float level = [self.audioPlayer peakPowerForChannel:0]; float alphaLevel = ((level * -1.0)/160);_ and it's giving me positive float numbers in the 0. to -1 range, however there is a distinct peak in volume everytime the NSTimer updates the UIView alpha.... hm.. – Splosion Lowbeam Jul 06 '13 at 10:23

1 Answers1

0

This has morphed into a problem with NSTimer and updating UIAnimations. I have 2 NSTimers in my ViewController: One of them, sliderTimer ,updates a progress slider at an interval of 1.0 (1 sec). The other, meterTimer, updates the UIAnimation of an alpha channel in UIView. The sliderTimer seems to be interferring with meterTimer.

In the simulator, it creates a blinking effect in my animation and in the log there is a repetitive peak in the float number at of the top each second. Here's the relevant parts of my code:

.h file

@property (weak, nonatomic) IBOutlet UIImageView *mouthMeter;
- (void)updateSlider:(id) sender;<br/>
- (void)updateMouthMeter:(id) sender;

.m file

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

-(void)updateSlider : (id)sender {
//Update progressSlider about the music time
self.circularSliderPlayback.value = self.audioPlayer.currentTime;
}

-(void)updateMouthMeter : (id)sender {
[self.audioPlayer updateMeters];
float level = [self.audioPlayer peakPowerForChannel:0];
float alphaLevel = ((level * -1.0)/160);

NSLog(@"this is the alphaLevel %f",alphaLevel);

[UIView beginAnimations:nil context:NULL];
self.mouthMeter.alpha = alphaLevel;
[UIView commitAnimations];
}

I'm new to iOS programming, is it a total no-no to have 2 NSTimers going in the same .m file? What are my options to get rid of this pulsing effect from sliderTimer?
Also, i've noticed sliderTimer is also affecting the audio being played back.. there is a quick pause at each second. What's goin' on?

Splosion Lowbeam
  • 73
  • 1
  • 1
  • 10