如何编辑SystemSoundID对象卷?使用SystemSoundID创造良好时,按下一个按钮
问题描述:
IM,这种方式:如何编辑SystemSoundID对象卷?使用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);
W帽子是控制效果声音的最佳方式?我想确保如果有人将他的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];