System sound becomes louder when AVAudioPlayer is initiated

那年仲夏 提交于 2019-12-11 04:27:00

问题


I've found 1 question on SO about the same issue and it remains unanswered. AVAudioPlayer affects system sounds

AVAudioSessionCategoryAmbient is not the answer as that only allows the app to play its audio simultaneously with another app's audio, that's not it.

I play sound effects such as button clicking sounds etc with SystemSound and background music with AVAudioPlayer. When AVAudioPlayer starts while SystemSound is being played, that particular system sound becomes like 5 times louder all of a sudden. Has anyone else experienced that? What could be the fix?

These are in SoundControl.m and I'm playing gameBG right after transition.

-(void)gameBG {

self.sharedSoundControl = [SoundControl sharedSoundControl];
if (self.sharedSoundControl.musicOff == NO) {
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"bgm" withExtension:@"mp3"] error:nil];
    self.audioPlayer.numberOfLoops = -1;
    self.audioPlayer.volume = 0.3f;
        [self.audioPlayer play];
}}

-(void)transition {

if (self.sharedSoundControl.effectOff == NO) {

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"transition" ofType:@"caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}}

EDIT

Just realized that if another instance of AVAudioPlayer is already being played anywhere, such behavior does not occur. It happens when there's no active AVAudioPlayer and you play system sound followed by AVAudioPlayer then voila, that system sound you just played became REALLY loud all of a sudden.

I'm thinking, if I get no answers, I'm just gonna have to play some random muted AVAudioPlayer right before playing system sound so this won't happen but I'm hoping to get some "real" fix for this. Please do not suggest to play some random muted AVAudioPlayer right before playing system sound because that's what I just said..

来源:https://stackoverflow.com/questions/37436868/system-sound-becomes-louder-when-avaudioplayer-is-initiated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!