【问题标题】:iOS Control Sound VolumeiOS 控制音量
【发布时间】:2016-03-26 16:11:26
【问题描述】:

我正在播放这样的声音:

#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
..
..
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle]
   pathForResource:@"ClickSound" ofType:@"wav"];    

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound (soundID);

它的音量似乎来自“铃声”,但是当我使用物理音量按钮时,它会控制“音量”(所以我可以“静音”音量 - 但我仍然听到声音)。

我想控制正确的音量,但我不想让它在静音时播放(顺便说一句 - 当我使用静音开关时它可以工作,但我听不到声音)。

我该如何解决?

【问题讨论】:

    标签: ios objective-c iphone ipad


    【解决方案1】:

    AudioServicesCreateSystemSound 仅适用于铃声音量。

    您可以为此使用 AVAudioPlayer。 下面是一些示例代码:

    AVAudioPlayer *buttonClick=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"buttonClick"] ofType:@"mp3"]] error:NULL];
    [buttonClick prepareToPlay];
    [buttonClick play];
    

    【讨论】:

      【解决方案2】:

      您无法控制代码中的音量。您可以提供一个 UI 控件来让用户更改音量。

      音频按类别管理,在尊重硬件设置方面的行为有所不同。

      Ambient 类别将支持静音开关;其他人不会。

      没有类别可以让您确定静音开关的位置。

      【讨论】:

      • 我想通过物理控制器来控制声音,他们控制“音量”,但我的游戏声音总是一样的。换句话说 - 我希望“音量”控制 AudioServicesPlaySystemSound() 函数而不是“铃声”。 @Avi
      • 你想要什么和你得到什么是不同的。如果任何愚蠢的应用程序可以随意设置音量,而我必须弄清楚可能有不同的控件,那将是一种糟糕的用户体验
      【解决方案3】:

      您可能应该使用以下代码注册音量按钮的回调,

      - (BOOL)application:(UIApplication *)application
      didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          [[NSNotificationCenter defaultCenter]
           addObserver:self
           selector:@selector(volumeChanged:)
           name:@"AVSystemController_SystemVolumeDidChangeNotification"
           object:nil];
      }
      
      - (void)volumeChanged:(NSNotification *)notification
      {
          float volume =
          [[[notification userInfo]
            objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
           floatValue];
      }
      

      希望它会有所帮助。 快乐编码:)

      【讨论】:

        猜你喜欢
        • 2013-04-03
        • 2013-09-03
        • 2011-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多