我试图让AVAudioPlayer
的音量随时间推移而振荡。
Ex:每隔10秒,将音量调整为0.5,然后调整为1.0,等等。
我尝试过使用NSTimer
,但它只在第一次运行时才会循环播放。
oscillateTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(oscillateRun) userInfo:nil repeats:YES];
oscillateRun function
- (void)oscillateRun {
BOOL oscillation;
oscillation = NO;
if(oscillation = YES) {
oscillation = NO;
audioPlayer.volume = 0.50;
}
else {
oscillation = YES;
audioPlayer.volume = 1;
}
}
不确定该怎么做,提前谢谢!
答案 0 :(得分:0)
这里problem
是每次你创建新的bool变量时,每次都没有“为什么值”AVPlayer
的音量没有变化。
全球创造振荡对象,
BOOL oscillation;
将此方法称为
[self oscillateRun];
- (void)oscillateRun {
if(oscillation == YES) {
oscillation = NO;
[audioPlayer setVolume : 0.50];
}
else {
oscillation = YES;
[audioPlayer setVolume : 1.0];
}
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(oscillateRun) userInfo:nil repeats:YES];
}
编辑: -
for camparing the two variables use '==' change this one in if condition then it'l works fine.