IOS listens to app volume changes
Method 1:
In the applicationdidfinishlaunching function, add
[[Nsicationcenter center defacenter center] addobserver: Self
Selector: @ selector (volumechanged :)
Name: @ "avsystemcontroller_systemvolumedidchangenotification"
Object: Nil];
-(Void) volumechanged :( nsnotification *) Notification
{
Float volume =
[[[Notification userinfo]
Objectforkey: @ "avsystemcontroller_audiovolumenotifparameter"]
Floatvalue];
Ddlogverbose (@ "current volume = % F", volume );
}
Disadvantage: When the app enters the next day, it will still listen to volume changes
2. Add a listener for volume changes to the audiosession, which can be placed in the startaudiosession function.
// Add a listener for outputvolume
Audiosessionaddpropertylistener (kaudiosessionproperty_currenthardwareoutputvolume,
Volumelistenercallback,
Self
);
Void volumelistenercallback (
Void * inclientdata,
Audiosessionpropertyid inid,
Uint32 indatasize,
Const void * indata
){
Const float * volumepointer = indata;
Float volume = * volumepointer;
Nslog (@ "volumelistenercallback % F", volume );
}
3. Obtain the current volume
Float volume = 0.0;
Uint32 datasize = sizeof (float );
Osstatus status = audiosessiongetproperty (kaudiosessionproperty_currenthardwareoutputvolume,
& Datasize,
& Volume );