- Home /
How to NOT play audio in silent mode with iOS?
Hi, my problem is as the title. I'm developing a casual app for Android and iOS. When on iOS devices, my app always plays sounds and BGM even in silent mode.
Does anyone know solutions of this problem? Any flags for silent mode? Any settings on xcode?
Thanks!
Answer by HHiro · Feb 04, 2015 at 09:34 AM
I found a way to not play audio in silent mode with iOS. I just redefined AudioSessionCategory.
On xcode, open YOURPROJECT/Classes/UI/UnityViewControllerBase.mm and add the follow in viewWillAppear.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
AppController_SendMainViewControllerNotification(kUnityMainViewWillAppear);
UInt32 category = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
}
Don't forget to include include AudioToolbox/AudioToolbox.h. I don't know this is a correct way, but it works.
$$anonymous$$y previous way is not proper for iOS7 or later. AudioSession is deprecated and AVAudioSession is better now.
(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
AppController_Send$$anonymous$$ainViewControllerNotification(kUnity$$anonymous$$ainViewWillAppear);
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioSession setActive:YES error:nil];
}
Don't forget to include AVFoundation.h ins$$anonymous$$d of AudioToolbox.h.
Thanks for sharing but, this isn't work my project.
I just change one row then, it works for IOS9+
[audioSession2 setCategory:AVAudioSessionCategoryPlayback error:nil];