AVAudioPlayer影响系统的声音 [英] AVAudioPlayer affects system sounds
问题描述
我用这个code打短的声音效果。发生这种情况时,用户点击键盘,这使得键盘点击声效果响比平常的返回按钮。是否有可能在发挥一定体积的效果,而不会影响系统的声音?
I use this code to play a short sound effect. This happens when user clicks the return button of the keyboard, which makes the keyboard click sound effect louder than usual. Is it possible to play the effect at certain volume without affecting system sounds?
- (void)playCorrectAnswerSound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"correct" ofType:@"mp3"];
NSURL *urlPath = [NSURL fileURLWithPath:path];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPath error:nil];
self.player.volume = 0.04;
[self.player prepareToPlay];
[self.player play];
}
推荐答案
中加入以下code到您的应用程序设置应用程序的音频会话类别 AVAudioSessionCategoryAmbient
委托的 applicationDidFinishLaunchingWithOptions:
方法:
Set your application's audio-session category to AVAudioSessionCategoryAmbient
by adding the following code to your app delegate's applicationDidFinishLaunchingWithOptions:
method:
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
[[AVAudioSession sharedInstance] setDelegate:self];
在
这是Apple的< href=\"http://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Configuration/Configuration.html#//apple_ref/doc/uid/TP40007875-CH3-SW1\"相对=nofollow> DOC :
该类别允许从iPod,Safari浏览器音响等内置
应用程序,而你的应用程序正在播放的音频播放。
This category allows audio from the iPod, Safari, and other built-in applications to play while your application is playing audio.
这篇关于AVAudioPlayer影响系统的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!