IOS growth path-detecting earphone insertion/removal

Source: Internet
Author: User
Tags call back

Importing Apple's two frameworks is essential...

CodePart + minor solution:

 

-(Void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib. audiosessioninitialize (null, null);/* osstatus audiosessioninitialize (cfrunloopref inrunloop, cfstringref inrunloopmode, audiosessioninterruptionlistener ininterruptionlistener, void * inclientdata); this function, you must call inrunloop the run loop that the interruption listener callback shocould be run on before calling other audiosession functions. pass null to use the main run loop. if this parameter is set to null, the default main run loop is used. (when the listener calls back, the loop is stopped.) inrunloopmode the mode for the run loop that the interruption listener function will run on. passing null is equivalent to passing kcfrunloopdefamode mode (kcfrunloopdefadefamode to hold objects, which are monitored when the application or thread is idle ). (When the listener is about to call back, the execution cycle is interrupted.) null = kcfrunloopdefaultmode, ininterruptionlistener the interruption listener callback function. the application's audio Session Object invokes the callback when the session is interrupted and (if the application is still running) when the interruption ends. can be null. see audiosessioninterruptionlistener. use null to replace audiosessioninterruptionlistener (audio Session interrupted). When we unplug the headset, the audio session is interrupted so that the application canProgram . Inclientdata data that you wowould like to be passed to your interruption listener callback. */[self addheadphonelistener];}

Add listener events and callback functions:

 

 

// Listen for earphone insertion and unplugging-(bool) addheadphonelistener {osstatus status = listener (listener, listener, self);/* listener (audiosessionpropertyid inid, audiosessionpropertylistener inproc, void * inclientdata) register a listener: audioroutechangelistenercallback. When the audio session transmission mode (headset/speaker) changes, the listener kaudiosessionproperty_audioroutechange will be triggered: is to check whether the audio route changes */} void encode (void * inuserdata, audiosessionpropertyid inpropertyid, uint32 inpropertyvalues, const void * inpropertyvalue) {uint32 propertysize = sizeof (cfstringref); encode (null, null, null, null); cfstringref state = nil; // obtain the audio route audiosessiongetproperty (kaudiosessionproperty_audioroute, & propertysize, & State); // kaudiosessionproperty_audioroute: audio route nslog (@ "% @", (nsstring *) State); // headphone headset speaker .}

I don't understand it thoroughly. Hope you can give me some advice.

 

 

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.