Ios NSTimer在AVAudioPlayer和UIAnimation中创建脉冲效果
我制作了一个带有Ios NSTimer在AVAudioPlayer和UIAnimation中创建脉冲效果,ios,objective-c,avaudioplayer,alpha,uianimation,Ios,Objective C,Avaudioplayer,Alpha,Uianimation,我制作了一个带有UIVIew.png图像的声音应用程序,我想根据AVAudioPlayer中的averagePowerForChannel对其alpha进行调暗。通过每秒12次的NSTimer,meterTimer更新来更改alpha(以模拟12 fps)。我还有第二个NSTimer,slider,它每秒更新一次UISlider的位置 暗化似乎正在发生,但阿尔法脉冲的第二个效应是完全打开(1.0)。当我弄乱n定时器的间隔时,我可以改变脉冲的节奏,因此我认为它们在某种程度上干扰,但我不知道如何或为
UIVIew
.png图像的声音应用程序,我想根据AVAudioPlayer
中的averagePowerForChannel
对其alpha进行调暗。通过每秒12次的NSTimer
,meterTimer
更新来更改alpha(以模拟12 fps)。我还有第二个NSTimer
,slider
,它每秒更新一次UISlider
的位置
暗化似乎正在发生,但阿尔法脉冲的第二个效应是完全打开(1.0)。当我弄乱n定时器的间隔时,我可以改变脉冲的节奏,因此我认为它们在某种程度上干扰,但我不知道如何或为什么
有什么想法吗?其他人是否有使用AVAudioPlayer
音频驱动和UIAnimation
的经验
h.file
m.file
不要为此使用动画。只需将self.moutmeter.alpha
设置为您的alphaLevel
。动画可能是闪烁的原因,因为运行动画所需的时间可能比0.12秒更新周期中的一个要长。另外,您是否检查了alphaLevel的输出?是否存在反映您所看到的内容的值?(alpha 0.0)@swissdude I删除了动画——效果相同。当NSTimer设置为0.12时,日志显示alpha通道每25次更新会精确闪烁至0.750000。在闪烁之间是0.01-0.16范围内的不同值,非常低…嗯!低电平可能只是声音文件,有点安静。我再试试。对不起,今天太长了。你用不同的声音文件成功了吗?您是否尝试使用频道1而不是频道2?([self.audioPlayer average powerforchannel:1]
)对不起,这里有点黑暗。。。
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property(getter = isMeteringEnabled) BOOL meteringEnabled;
@property (nonatomic, strong) NSTimer *sliderTimer;
@property(nonatomic,strong) NSTimer * meterTimer;
...
- (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];
}