耳機線控
1.引入
#import <AVFoundation/AVFoundation.h>
2.開啟遠端控制事件
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; if (![[AVAudioSession sharedInstance] setActive:YES error:nil]) { NSLog(@"Failed to set up a session"); } [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
3.調用遠端控制方法
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{ NSLog(@"event type: %d, subtype:%d", event.type, event.subtype); if (event.type == UIEventTypeRemoteControl) { switch (event.subtype) { case UIEventSubtypeRemoteControlPlay: NSLog(@"播放"); break; case UIEventSubtypeRemoteControlPause: NSLog(@"暫停"); break; case UIEventSubtypeRemoteControlStop: NSLog(@"停止"); break; case UIEventSubtypeRemoteControlTogglePlayPause: NSLog(@"暫停/停止"); break; case UIEventSubtypeRemoteControlNextTrack: NSLog(@"下一曲"); break; case UIEventSubtypeRemoteControlPreviousTrack: NSLog(@"上一曲"); break; case UIEventSubtypeRemoteControlBeginSeekingBackward: NSLog(@"開始後退"); break; case UIEventSubtypeRemoteControlEndSeekingBackward: NSLog(@"結束後退"); break; case UIEventSubtypeRemoteControlBeginSeekingForward: NSLog(@"開始前進"); break; case UIEventSubtypeRemoteControlEndSeekingForward: NSLog(@"結束前進"); break; default: break; } }}