- Home /
Getting iOS device volume
Hi,
I am trying to figure out a way to retrieve the current volume from an iOS device. I don't need to change it...just retrieve it. I thought about using a plugin for this, but I don't really want to poll the device, but rather have the device fire an event that talks back to Unity via the UnitySendMessage callback.
I found the following code bits while researching the issue, but I can't seem to get them to work properly as I really have no idea where they are supposed to go.
// Volume control notification
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
- (void) volumeChanged:(NSNotification *) notification
{
float volume = [[[notification userInfo]
objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
floatValue];
NSString *volString = [NSString stringWithFormat: @"%f",volume];
const char* volChar = [volString UTF8String];
UnitySendMessage("AudioManager","OnVolumeChanged",volChar);
}
I have tried putting the event registration in various places around the AppController.mm, but it never seems to fire the actual event. My Obj-C is very weak, so I'm sure I'm doing something blatantly wrong. Anybody have any suggestions?
Answer by agamedesigner · Aug 03, 2012 at 08:13 PM
I think this might help: http://stackoverflow.com/questions/7207417/how-to-detect-an-iphone-ipod-volume-level
I did run across this in my search and this would be an alternate way of getting the device volume (there were several I found), but how do I tie that into an event that is fired when someone actively changes the volume on the device?
I think I already have that part of it down. I did add the following right above my notification registration code, but it didn't seem to make a difference, which I put in the startUnity function.
AudioSessionInitialize(NULL, NULL, NULL, NULL); AudioSessionSetActive(true);