iOS Headset Wire control

Source: Internet
Author: User

When you use the iphone when listening to music, the player in the background when running, you can still operate through the headset to complete the track switch, Fast forward, rewind and other functions!
Of course your program is not necessarily a player app, but we can still let it have this function, let the user through the headset to do some of the more simple common operations, so is not cool?
How to achieve it? Nonsense not much to say, we went straight to the subject:
1, allow remote events to be accepted

[[UIApplication sharedapplication] beginreceivingremotecontrolevents];
[Self becomefirstresponder];

2, Handling Input events:

-(void) Remotecontrolreceivedwithevent: (uievent *) receivedevent {

if (Receivedevent.type = = Uieventtyperemotecontrol)

{

Switch (receivedevent.subtype) {

Case Uieventsubtyperemotecontroltoggleplaypause:

Do something

Break

Case Uieventsubtyperemotecontrolprevioustrack:

Do something

Break

Case Uieventsubtyperemotecontrolnexttrack:

Do something

Break

Default

Break

}

}

}

3, stop accepting remote events when use is complete

[[UIApplication sharedapplication] endreceivingremotecontrolevents];
[Self resignfirstresponder];

4, attach the complete event type code for everyone to use

typedef enum {

Uieventsubtypenone = 0,

Uieventsubtypemotionshake = 1,

Uieventsubtyperemotecontrolplay = 100,

Uieventsubtyperemotecontrolpause = 101,

Uieventsubtyperemotecontrolstop = 102,

Uieventsubtyperemotecontroltoggleplaypause = 103,

Uieventsubtyperemotecontrolnexttrack = 104,

Uieventsubtyperemotecontrolprevioustrack = 105,

Uieventsubtyperemotecontrolbeginseekingbackward = 106,

Uieventsubtyperemotecontrolendseekingbackward = 107,

Uieventsubtyperemotecontrolbeginseekingforward = 108,

Uieventsubtyperemotecontrolendseekingforward = 109,

} Uieventsubtype;

To supplement, reprint:

Recently found someone in the jar post for iOS headset cable control events, the following to share the code for everyone,



-(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 the 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-click Pause key: 105");
}break;
Case uieventsubtyperemotecontrolbeginseekingforward:{
NSLog (@ "Click, then press not put: 108");
}break;
Case uieventsubtyperemotecontrolendseekingforward:{
NSLog (@ "Click, press Again, Release: 109");
}break;
Default
Break
}
}
}

iOS Headset Wire control

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.