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