- Home /
How to avoid 'auto audio volume decrease' on iphone?
As gamer, I usually listen to podcast with bluetooth while playing games on my iPhone.
I prefer to set audio volume settings to 20% BGM, 50% SFX volume and device master volumes to best-fit for podcast.
So, I can clearly listen to what podcast saying, but not killing all the moods of game by mute.
I'm disturbed when I found my game(in-dev) automatically cut my podcast volume about half when it launches on my iPhone.
I googled and found someone saying iOS automatically adjust audio to app's audio data to protect ear and to avoid hearing problem for being exposed to loud sound.
So, I tried change all volumes of sound clips, but it doesn't help. And I played some recent games that might be unity-powered, also show same situation, which I didn't see this before. ( Tinker Island, Dungeon Warfare, Build Away! and such.. )
I've been awful experiences with them because I need to change device master volume every time when I turn on and off games, with sudden loud surprises.
As developer, what I want to do is prevent this from my game.
Is there any particular settings to avoid this?
Is this something new bug of latest Unity?
I noticed this 'reduced volume' during 5.4 beta-period(with my in-dev game),
and currently using release version of 5.4.0.3 (no patch applied.) and iPhone 6+ with iOS 9.3.4.
Did someone notice this? Does it bother or not?
I noticed the problem aswell. Very annoying. I'm pretty sure it is a Unity bug. Haven't yet found a way to prevent this, sadly.
At first, I thought It might be UnityAds's movie volume was the main cause of this problem, but my new project (for next prototype idea) without UnityAds also has this problem.
Answer by lanestp · Sep 30, 2016 at 05:03 PM
I had this problem, VERY irritating. I fixed it by adding a plugin that swizzles AVAudioSession. Here's my code, I hope it helps.
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <objc/runtime.h>
@interface AudioFix : NSObject
@end
__strong AudioFix *sharedInstance;
@implementation AVAudioSession(AudioFix)
+ (void)load {
sharedInstance = [[self alloc] init];
}
- (BOOL)newsetCategory:(NSString *)category
withOptions:(AVAudioSessionCategoryOptions)options
error:(NSError * _Nullable *)outError {
options = AVAudioSessionCategoryOptionMixWithOthers;
return [self newsetCategory:category withOptions:options error:outError];
}
- (id)init {
if (!(self = [super init])) return nil;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserverForName:UIApplicationDidFinishLaunchingNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL setcategory = @selector(setCategory:withOptions:error:);
SEL newsetcategory = @selector(newsetCategory:withOptions:error:);
Method originalMethod = class_getInstanceMethod([self class], setcategory);
Method extendedMethod = class_getInstanceMethod([self class], newsetcategory);
method_exchangeImplementations(originalMethod, extendedMethod);
});
}];
return self;
}
@end
Thanks for this. It worked for us, but was crashing on iOS 9.3.4, I think because the original AVAudioSession init method wasn't being called. Have added our own solution based on yours as an answer here too.
Answer by Bill-Ape · Feb 22, 2017 at 04:25 PM
Based on @lanestp's answer, we were finding iOS 9.3.4 was crashing with this. So we modified it thus and it worked. Thanks
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <objc/runtime.h>
@implementation AVAudioSession(UnityStartupVolumeFix)
- (BOOL)altSetCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError * _Nullable *)outError
{
options = AVAudioSessionCategoryOptionMixWithOthers;
return [self altSetCategory:category withOptions:options error:outError];
}
+ (void)load
{
AVAudioSession* instance = [AVAudioSession sharedInstance];
static dispatch_once_t avAudioSessionHackOnce;
dispatch_once(&avAudioSessionHackOnce, ^{
SEL setCategorySelector = @selector(setCategory:withOptions:error:);
SEL altSetCategorySelector = @selector(altSetCategory:withOptions:error:);
Method originalMethod = class_getInstanceMethod([instance class], setCategorySelector);
Method extendedMethod = class_getInstanceMethod([instance class], altSetCategorySelector);
method_exchangeImplementations(originalMethod, extendedMethod);
});
}
@end
Answer by pmaloka · Sep 07, 2016 at 06:32 PM
@tinklife, @thorn-erikson This is a known issue. Please upvote so they fix it sooner.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091520im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting iOS device volume 1 Answer
what audio.volume level on iphone would match the music player volume? 1 Answer
DSPBuffer Size from script 1 Answer
Math formula to set volume levels nicely 0 Answers
Audio not coming out of speakers 1 Answer