Sounds in UILocalNotification louder than AVAudioPlayer at max volume

旧时模样 提交于 2020-01-03 03:13:06

问题


For purposes of this question, imagine my app plays an audio clip every 10 seconds. This audio plays/mixes with the iPod music player on the device (using ducking), using AVAudioPlayer. When the app is sent to the background, I schedule UILocalNotification objects with the audio file referenced (and no text), so the sounds continue to play at 10 second intervals.

What is bothering me is that the volume of the audio clips played as part of a notification on iOS 6 seem to be twice as loud as the audio when I play in my app (and I'm setting the volume to 1.0f, which the docs say is max). So every 10 seconds the app plays a sound, and when you send to the background it's now very loud compared to what it was in the app.

Relevant snippets... App startup, here's how I'm setting up the AVAudioSession to enable ducking:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);

OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof (allowMixing),&allowMixing);

... How I create my player:

- (AVAudioPlayer *)playerWithCAFFileNamed:(NSString *)fname {
    NSURL *u = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:fname ofType:@"caf"]];
    NSData *d = [NSData dataWithContentsOfURL:u];
    AVAudioPlayer *p = [[AVAudioPlayer alloc] initWithData:d error:nil];
    p.delegate = self;
    p.volume = 1.0; 
    [p prepareToPlay];
    return p;
}

... and then the playback:

- (void)playAudio:(AVAudioPlayer *)player {
    [self setSessionActiveWithMixing:YES];
    [player setVolume:1.0]; // should already be set, but just to be sure
    [player play];
}

... and here's how I'm creating the uber-loud notifications:

- (UILocalNotification *)notificationWithSoundNamed:(NSString *)sound atTimeInterval:(NSTimeInterval)ti {
    UILocalNotification *n = [[UILocalNotification alloc] init];
    n.soundName = sound;
    n.fireDate = [NSDate dateWithTimeIntervalSinceReferenceDate:ti];
    return n;
}

回答1:


The bug I opened with Apple was closed as "functions as designed". Their brief explanation:

Notification sounds follow ringer volume; AVAudioPlayer follows media volume.

... I guess you can then make the assumption that ringer volume is allowed to be louder than the loudest media volume, to ensure you hear the ringer. I guess.




回答2:


I faced similar issue with AVAudioPlayer volume. So I divided the value I want to assign by a factor of what I want to get. Worth finding the reason though.



来源:https://stackoverflow.com/questions/14549067/sounds-in-uilocalnotification-louder-than-avaudioplayer-at-max-volume

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