我使用此代码播放简短的声音效果。当用户单击键盘的返回按钮时会发生这种情况,这会使键盘单击声音效果比平时响亮。是否可以在不影响系统声音的情况下以一定的音量播放效果?
- (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];
}
最佳答案
将应用程序的 Audio Session 类别设置为 AVAudioSessionCategoryAmbient
通过将以下代码添加到您的应用程序委托(delegate)的 applicationDidFinishLaunchingWithOptions:
方法:
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
[[AVAudioSession sharedInstance] setDelegate:self];
来自苹果的doc :
This category allows audio from the iPod, Safari, and other built-in applications to play while your application is playing audio.
关于iphone - AVAudioPlayer 影响系统声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299493/