Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

ios - NSTimer creating a pulse effect in AVAudioPlayer and UIAnimation

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];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...