在Xcode中振荡AVAudioPlayer卷

时间:2013-05-07 04:44:36

标签: ios xcode avaudioplayer nstimer nstimeinterval

我试图让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;
        }

}

不确定该怎么做,提前谢谢!

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.