Get the iOS Headset wire-control event

Source: Internet
Author: User
-(BOOL) canbecomefirstresponder{
NSLog (@ "_____%s_____", __function__);
return YES;
}


Received remote event
-(void) Remotecontrolreceivedwithevent: (uievent *) event{
NSLog (@ "event tyipe:::%d subtype:::%d", Event.type,event.subtype);
type==2 subtype== Click Pause Key: 103, double-click Pause Key 104
if (Event.type = = Uieventtyperemotecontrol) {
Switch (event.subtype) {

Case uieventsubtyperemotecontrolplay:{
NSLog (@ "Play---------");
}break;
Case uieventsubtyperemotecontrolpause:{
NSLog (@ "Pause---------");
}break;
Case uieventsubtyperemotecontrolstop:{
NSLog (@ "Stop---------");
}break;
Case uieventsubtyperemotecontroltoggleplaypause:{
Click Pause Key: 103
NSLog (@ "Click Pause key: 103");
}break;
Case uieventsubtyperemotecontrolnexttrack:{
Double-click Pause key: 104
NSLog (@ "Double-click Pause key: 104");
}break;
Case uieventsubtyperemotecontrolprevioustrack:{
NSLog (@ "Three hit pause key: 105");
}break;
Case uieventsubtyperemotecontrolbeginseekingforward:{
NSLog (@ "Click, then press not put: 108");
}break;
Case uieventsubtyperemotecontrolendseekingforward:{
NSLog (@ "Click, then press not to put, loosen when: 109");
}break;
Default
Break
}
}
}


Add the above code to get the headset line control of each click event, tick the taxi before the version of a headset grab a single function, is so realized



====================================================================================================



Capture Headphone Plug event in iOS development

void Audioroutechangelistenercallback (
void *inuserdata,
Audiosessionpropertyid InID,
UInt32 Indatasize,
const void *indata)
{
UInt32 propertysize = sizeof (CFSTRINGREF);
Audiosessioninitialize (NULL, NULL, NULL, NULL);
Cfstringref state = nil;
Audiosessiongetproperty (Kaudiosessionproperty_audioroute
, &propertysize,&state);

NSLog (@ "%@", (NSString *) state);//return @ "headphone" or @ "Speaker" and "."
}
-(void) Viewdidload {
[Super Viewdidload];

Audiosessioninitialize (NULL, NULL, NULL, NULL);
Osstatus status = Audiosessionaddpropertylistener (
Kaudiosessionproperty_audioroutechange,
Audioroutechangelistenercallback,self);
if (status = = 0) {//ok}
}


====================================================================================================

First Import System class library

#import <AVFoundation/AVFoundation.h>

Monitor Headphone Events

[[Avaudiosessionsharedinstance] setdelegate:self];

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

[[Avaudiosessionsharedinstance] setCategory:AVAudioSessionCategoryPlaybackerror:nil];

Registers the audio route Change listener callback function

Audiosessionaddpropertylistener (Kaudiosessionproperty_audioroutechange,audioroutechangelistenercallback, self);

Write this code where you need to listen.


I personally recommend to put

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOption

Because it's the big picture, where you can respond to it.


For example


-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

{

Self.window = [[[Uiwindowalloc] initwithframe:[[uiscreenmainscreen]bounds]]autorelease];

Landing VC

Loginviewcontroller *LOGINVC = [[Loginviewcontrolleralloc]init];

Uinavigationcontroller *NAVNV = [[UINAVIGATIONCONTROLLERALLOC]INITWITHROOTVIEWCONTROLLER:LOGINVC];

[Navnvsetnavigationbarhidden:yes];

[LOGINVC release];

Monitor Headphone Events

[[Avaudiosessionsharedinstance] setdelegate:self];

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

[[Avaudiosessionsharedinstance] setCategory:AVAudioSessionCategoryPlaybackerror:nil];

Registers the audio route change listener

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.