[[Uidevice currentdevice] setproximitymonitoringenabled: Yes]; // we recommend that you set yes before playing and no after playing. This function enables infrared sensing.
// Add a listener
[[Nsicationcenter center defacenter center] addobserver: Self
Selector: @ selector (sensorstatechange :)
Name: @ "uideviceproximitystatedidchangenotification"
Object: Nil];
// Process listener-triggered events
-(Void) sensorstatechange :( nsicationicationcenter *) notification;
{
// If your phone is near your face and placed near your ears, the audio will be output through the receiver and the screen will be dimmed (power-saving)
If ([[uidevice currentdevice] proximitystate] = Yes)
{
Nslog (@ "device is close to user ");
[[Avaudiosession sharedinstance] setcategory: avaudiosessioncategoryplayandrecord error: Nil];
}
Else
{
Nslog (@ "device is not close to user ");
[[Avaudiosession sharedinstance] setcategory: avaudiosessioncategoryplayback error: Nil];
}
}
// Set the following when initializing the player:
Uint32 sessioncategory = kaudiosessioncategory_mediaplayback;
Audiosessionsetproperty (kaudiosessionproperty_audiocategory,
Sizeof (sessioncategory ),
& Sessioncategory );
Uint32 audiorouteoverride = kaudiosessionoverrideaudioroute_speaker;
Audiosessionsetproperty (kaudiosessionproperty_overrideaudioroute,
Sizeof (audiorouteoverride ),
& Audiorouteoverride );
Avaudiosession * audiosession = [avaudiosession sharedinstance];
// Speaker playing by default
[Audiosession setcategory: avaudiosessioncategoryplayback error: Nil];
[Audiosession setactive: Yes error: Nil];