In iOS, how does one listen to the headset insertion and unplugging event?

Source: Internet
Author: User
First, import the system library

# Import <avfoundation/avfoundation. h>

// Listen to the headset event

[[Avaudiosessionsharedinstance] setdelegate: Self];

// Use this code instead to allow the app sound to continue to play when the screen is locked.

[[Avaudiosessionsharedinstance] setcategory: avaudiosessioncategoryplayback error: Nil];

// Registers the audio route change listener callback function

Audiosessionaddpropertylistener (kaudiosessionproperty_audioroutechange, audioroutechangelistenercallback, self );

Write this code to the location where you want to listen

I personally recommend

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoption

Because it is a global solution, everything can be responded

For example

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions

{

Self. Window = [[[uiappswalloc] initwithframe: [[uiscreenmainscreen] bounds] autorelease];

// Log on to VC

Loginviewcontroller * loginvc = [[loginviewcontrolleralloc] init];

Uinavigationcontroller * navnv = [[uinavigationcontrolleralloc] initwithrootviewcontroller: loginvc
];

[Navnvsetnavigationbarhidden: Yes];

[Loginvc release];

// Listen to the headset event

[[Avaudiosessionsharedinstance] setdelegate: Self];

// Use this code instead to allow the app sound to continue to play when the screen is locked.

[[Avaudiosessionsharedinstance] setcategory: avaudiosessioncategoryplayback error: Nil];

// Registers the audio route change listener callback function

Audiosessionaddpropertylistener (kaudiosessionproperty_audioroutechange, audioroutechangelistenercallback, self );

Self. Window. rootviewcontroller = navnv;

[Navnv release];

Self. Window. backgroundcolor = [uicolorwhitecolor];

[Self. windowmakekeyandvisible];

Return yes;

}

// Triggered listening event

Void audioroutechangelistenercallback (void * inuserdata, audiosessionpropertyid inpropertyid, uint32 inpropertyvaluesize, const void * inpropertyvalue
){

// Ensure that this callback was invoked for a route change

If (inpropertyid! = Kaudiosessionproperty_audioroutechange) return;

{

// Determines the reason for the route change, to ensure that it is not

// Because of a category change.

Cfdictionaryref routechangedictionary = (cfdictionaryref) inpropertyvalue;

Cfnumberref routechangereasonref = (cfnumberref) cfdictionarygetvalue (routechangedictionary, cfstr (kaudiosession_audioroutechangekey_reason ));

Sint32 routechangereason;

Cfnumbergetvalue (routechangereasonref, kcfnumbersint32type, & routechangereason );

If (routechangereason = kaudiosessionroutechangereason_olddeviceunavailable ){

// Handle headset unplugged

Dlog (@ "no headphones! ");

} Else if (routechangereason = kaudiosessionroutechangereason_newdeviceavailable ){

// Handle headset plugged in

Dlog (@ "with headphones! ");

}

}

}

OK!

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.