Solution for switching between the receiver and speaker for WeChat voice playback in IOS

Source: Internet
Author: User

[[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];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.