【问题标题】:Oscillating AVAudioPlayer Volume in Xcode在 Xcode 中振荡 AVAudioPlayer 音量
【发布时间】:2013-05-07 04:44:35
【问题描述】:

我试图让AVAudioPlayer 的音量随时间波动。

例如:每 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;
        }

}

不知道该怎么做,在此先感谢!

【问题讨论】:

  • 当你这样尝试时发生了什么?

标签: ios xcode avaudioplayer nstimer nstimeinterval


【解决方案1】:

这里problem 是每次您创建新的布尔变量并且每次都需要NO 这'为什么值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.

【讨论】:

  • 我在哪里放置 [self oscillateRun] ?我的 NSTimer 目前在 audioPlayer 函数中,您建议将其移至 oscillateRun 函数吗?
  • @Steven Ritchie:你可以把这个放在你想启动 AVPlayer(播放音频)的地方。
  • 这仍然不起作用。它在第一个间隔后降低音量,但在另一个间隔后不提高音量。我还使用了 if(oscillation == YES),这是我最初代码中的一个错字
  • @Steven Ritchie:一旦检查我的答案,它现在就可以正常工作了。
  • 我自己想出来的,把定时器放在函数中是并行创建定时器的多个实例,破坏它。不过谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-05-02
  • 2013-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
相关资源
最近更新 更多