Hi, I have some questions about silent button on iPhone device. I have some trouble with handle music and sounds in my game. The problem occurred in unity < 5.3 (current I have 5.3.2p4) before all works great, now when I switching the button in game I don’t see any change. Music still play. Do you have any idea what is happened? Is any way to handle this button from code ?
I actually found a solution for the problem, though it is done in xCode directly: click.
The problem is for me that I always override my xCode project, so I’ve got to find a solution how I can extend the UnityViewController with that code, but I tested it and it worked.
Hope that helps. If I find a way to extend the UnityViewController I’ll write again.
Ok, got it. Just in case anyone is interested in solving this issue (not sure why only so few people are complaining about it), I post the solution that worked for us:
1.
You have to extend the UnityAppController class of the xCode project and register to the AVAudioSession to get noticed by the change of the mute button.
To extend the UnityAppControlller class, simply create a new class under “Assets/Plugins/iOS” and name it “MyUnityAppController.mm”. NOTE: if you already have a script which extends the UnityAppController, use that one!
3.
The file will be automatically included into your xCode project (at least from Unity 5.x. Haven’t tested with 4.x). Check your xCode project if your file is present. Should be located under “Libraries/Plugins/iOS/MyApplicationController.mm”. Compile and build the app. When the game starts, you should see in the xCode log output the “MyUnityAppController Set audiosession” output.
Hi, I came across this post for a completely opposite reason.
For me the mute button is functioning properly in the game, i.e. on iPad if the mute button is ON, there is no sound in the game. I am facing an issue, where we have decided to override the mute button.
So in our game I want to not honor the mute button, how can I do this?
I am using Unity 5.3.5f1 and xcode 7.3.
Is no one having solution for this?
I really wish to keep playing the sound in my game, irrespective of the state of the mute button, as lot of our players keep getting sending us messages that “sound is not playing”, and we need to constantly remind them that there side switch, which is configured to mute is On, and turning it Off will resolve the issue.
A lil late … but I had the same problem today. As pahe posted, your can add your own MyUnityAppController.mm in that mentioned folder and it will copied and compiled in your exported project with Xcode. Instead of setting the category to:
Now, your Unity app still plays sound even if the silent switch is set to off. There are some pages at Apple developer, where the different modes and their behaviour in relation to the silent switch are explained.
If you want to listen to your sound even when the app is not in focus, have a look at the background modes in your plist.
Note: Usually the user wants to have his device silent when he is setting the switch. It can be a reason to get rejected by Apple, but it happens really rare, as far as I read. So use it with care.
I tried that solution but seems that doesn’t work in Unity 2017.1/2/3 and xCode 9.2. Seems MyUnityAppController not loaded on app start because there is not NSLog at all. What I did is created methods to change AVAudioSession properties and that worked fine.
Any suggestions why original approach doesn’t work now?
I encountered many times the problem, that a third party plugin overwrite starting functions or classes of Unity. That caused me the most head aches to figure out.
So, if you use 3rd party plugins, look for other scripts which may overwrite the UnityController class.
Facing the same issue with Vuforia here. Have you solved the problem and if yes, how?
I moved the code from MyUnityAppController.mm to VuforiaNativeRendererController.mm and can see the NSLog statements but i still can not hear any audio when i switch on the hardware mute button.
/*============================================================================
Copyright (c) 2017 PTC Inc. All Rights Reserved.
Copyright (c) 2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
============================================================================*/
#import "UnityAppController.h"
#import "UnityView.h"
#import "VuforiaRenderDelegate.h"
#import "AVFoundation/AVFoundation.h"
// Unity native rendering callback plugin mechanism is only supported
// from version 4.5 onwards
#if UNITY_VERSION>434
// Exported methods for native rendering callback
extern "C" void VuforiaSetGraphicsDevice(void* device, int deviceType, int eventType);
extern "C" void VuforiaRenderEvent(int marker);
#endif
// Controller to support native rendering callback
@interface VuforiaNativeRendererController : UnityAppController
{
}
- (void)shouldAttachRenderDelegate;
- (void)setAudioSession;
@end
@implementation VuforiaNativeRendererController
-(void) startUnity: (UIApplication*) application
{
NSLog(@"VuforiaNativeRendererController startUnity");
[super startUnity: application]; //call the super.
[self setAudioSession];
}
- (void)setAudioSession
{
NSLog(@"VuforiaNativeRendererController Set audiosession");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioSession setActive:YES error:nil];
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
BOOL ret = [super application:application didFinishLaunchingWithOptions:launchOptions];
if (ret)
{
_unityView.backgroundColor = UIColor.clearColor;
}
return ret;
}
- (void)shouldAttachRenderDelegate
{
self.renderDelegate = [[VuforiaRenderDelegate alloc] init];
// Unity native rendering callback plugin mechanism is only supported
// from version 4.5 onwards
#if UNITY_VERSION>434
UnityRegisterRenderingPlugin(&VuforiaSetGraphicsDevice, &VuforiaRenderEvent);
#endif
}
@end
IMPL_APP_CONTROLLER_SUBCLASS(VuforiaNativeRendererController)
UPDATE
I somehow skipped cyliax’s very useful AVAudioSessionCategoryPlayback advice.
we now run in the same problem using Vuforia. And the solution is very simple.
Don´t extend the UnityAppController or change the VuforiaNativeRendererController. Just add a native iOS script, which does the same and this in a more simple way:
Put the file in Unity to Assets/Plugins/iOS or something like Assets/our_custom_library_with_cool_scripts/Plugins/iOS
Or even better …/Plugins/AudioSessionSetter/iOS
Then you need a C# Script to call the native script. Let´s name it AudioSessionSetter.cs and put it one folder above the mm-file (in our case in …/Plugins/AudioSessionSetter):
As you see, the script is a MonoBehaviour. You can place it on a GameObject in your first scene and in Awake it will handle the AudioSessionSetting for you. No more coding needed. Of course, the scripts can be extended to enable or disable the feature I think. Just as you want. There is no need to fire this in an extended version of the UnityAppController → startUnity Class and Method.
The solution worked for us with older iOS (10.3) and the newer version also (12.2. and 12.3.x)