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