0

我使用 SystemSoundID 在按下按钮时创建声音,这样:

这是对象声明:

SystemSoundID soundEffect;

这是在 viewDidLoad 中:

NSString *soundPath = [[NSBundle mainBundle]pathForResource:@"create_button_sound" ofType:@"mp3"];

NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

AudioServicesCreateSystemSoundID(CFBridgingRetain(soundURL), &soundEffect);

最后当按下按钮时:

AudioServicesPlaySystemSound(soundEffect);

控制效果声音的最佳方法是什么?我想确保如果有人将他的 iphone 音量设置为最大,这样声音就不会太响亮..

谢谢@@!

4

1 回答 1

1

According to this SO question (AudioServicesPlaySystemSound Volume? -- slightly out of date since Sounds is no longer under General), you're going to get stuck because of a global setting that can override volumes for system sounds.

The workaround is to use AVAudioPlayer instead, if that's a possibility.

Example:

    NSString *soundPath = [[NSBundle mainBundle]pathForResource:@"create_button_sound" ofType:@"mp3"];

    NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
    AVAudioPlayer *mySoundPlayer =[[AVAudioPlayer alloc] soundURL error:&error];
    mySoundPlayer .volume=0.8f; //between 0 and 1
    [mySoundPlayer prepareToPlay];
    mySoundPlayer.numberOfLoops=0; //or more if needed

    [mySoundPlayer play];
于 2015-01-09T22:33:01.313 回答