First reprint several technical articles, to make the record
iOS Development Series--touch events, gesture recognition, shaking events, headphone wire control http://www.cnblogs.com/kenshincui/p/3950646.html
The Uiresponder of iOS framework research objects that can receive events (i)
http://blog.csdn.net/maojudong/article/details/7798138
iOS headphone cable Control http://blog.csdn.net/slinloss/article/details/18085145
Bottom start
The implementation of the above 3 articles reproduced in the article, you can refer to
Questions about the Remotecontrolreceivedwithevent method not executing:
1. Enable remote event reception ( using [[UIApplication sharedapplication] beginreceivingremotecontrolevents]; method).
2. The same requirement for UI controls must be the first responder (not required for view controller Uiviewcontroller or application UIApplication object monitoring).
3. The application must be the controller of the current audio, that is, the current audio playback program in the notification bar in iOS 7 must be our own development program.
The first two are not many, mainly speaking of the third, I will be the above "iOS headset wire control" This article in the code written in the
Appdelegate method, but does not execute the Remotecontrolreceivedwithevent method (Note: This method only can be triggered by the middle of headphones), and finally added a section of audio playback code, namely:
NSString *string = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "MP3"];
Convert audio files to URL format
Nsurl *url = [Nsurl fileurlwithpath:string];
Initialize the audio class and add the playback file
Self.avaudioplayer = [[Avaudioplayer alloc] Initwithcontentsofurl:url Error:nil];
Pre-playback
[_avaudioplayer Preparetoplay]; And then press the middle key of the headset to trigger the Remotecontrolreceivedwithevent method.
If some students say do not want to use headphones control playback audio, want to control camera photo, my plan is to put an empty audio file, the Remotecontrolreceivedwithevent method triggered, and then write their own custom method. (here do not want to use headphones to control the playback of audio, but to achieve control of other custom methods, if you have different options, you can share the message, learn together)