>我使用 SystemSoundID 在按下按钮时创建声音,方式如下:
这是对象声明:
SystemSoundID soundEffect;
这是在视图中DidLoad:
NSString *soundPath = [[NSBundle mainBundle]pathForResource:@"create_button_sound" ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(CFBridgingRetain(soundURL), &soundEffect);
最后,当按下按钮时:
AudioServicesPlaySystemSound(soundEffect);
控制效果声音的最佳方法是什么?我想确保如果有人将他的iPhone音量设置为最大,这样声音就不会变得疯狂响亮。
thanks@@!
根据这个SO问题(AudioServicesPlaySystemSound Volume?--由于Sounds不再在"常规"下,因此略微过时),您将陷入困境,因为全局设置可以覆盖系统声音的音量。
解决方法是改用AVAudioPlayer
,如果可能的话。
例:
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];