First include these two header files and join the corresponding framework
#import <MediaPlayer/MediaPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
To add a listener for a sound notification
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (volumechanged:) name:@ " Avsystemcontroller_systemvolumedidchangenotification "Object:nil";
get current system volume
Self.volume = [[Mpmusicplayercontroller Applicationmusicplayer] volume];
Determine if Mute
-(BOOL) ismuted
{
cfstringref route;
UInt32 routesize = sizeof(cfstringref);
osstatus status = Audiosessiongetproperty (Kaudiosessionproperty_audioroute, &routesize, &route);
if (status = = Kaudiosessionnoerror)
{
if (route = = NULL | |! Cfstringgetlength (route))
return TRUE;
}
return FALSE;
}
implementation of voice notification monitoring
-(void) volumechanged: (nsnotification *) notification
{
Gets the value of the current sound
float volume =
[[[Notification userInfo]
objectforkey:@ "Avsystemcontroller_audiovolumenotificationparameter"]
Floatvalue];
NSLog (@ " now Volume:%f", volume);
if (! Self. ismuted && Volume < self. Volume && self. Acceptbtn.hidden = = NO ) {
Set the volume to mute
[[Mpmusicplayercontroller Applicationmusicplayer] SetVolume:0.0];
adding system Vibrations
Audioservicesremovesystemsoundcompletion (ksystemsoundid_vibrate);
NSLog (@ "----ismuted volume :%f-------", volume);
} Else if (Volume > self. Volume && self. Acceptbtn.hidden = = no< c20>) {
[[Mpmusicplayercontroller Applicationmusicplayer] setvolume:volume];
NSLog (@ "----! ismuted volume :%f-------", volume);
}
// update the variable that records the volume value
self. Volume = volume;
}
IOS Voice key monitoring and implementation