RemoteControl
RemoteComtrol可以用来在不打开app的情况下控制app中的多媒体播放行为,涉及的内容主要包括:
1、锁屏界面双击Home键后出现的播放操作区域
2、iOS7之后控制中心的播放操作区域
3、iOS7之前双击home键后出现的进程中向左滑动出现的播放操作区域
4、AppleTV,AirPlay中显示的播放操作区域
5、耳机线控
6、车载系统的设置
RemoteControl的处理,根据官方文档说明,需要满足三个条件:
1、接收者必须可以成为第一响应者;
2、接收者必须显示声明接收RemoteControl事件;
3、App必须是Now Playing App.
对于第一种,我们给UIApplication添加一个扩展:
//
// UIApplication+RemoteControl.h
// IOSAudioRemoteControl
//
// Created by huangyibiao on 15/3/25.
// Copyright (c) 2015年 huangyibiao. All rights reserved.
//
#import <UIKit/UIKit.h>
// 播放
extern NSString *kRemoteControlPlayTapped;
// 暂停
extern NSString *kRemoteControlPauseTapped;
// 停止
extern NSString *kRemoteControlStopTapped;
// 前一首
extern NSString *kRemoteControlPreviousTapped;
// 后一首
extern NSString *kRemoteControlNextTapped;
// 其它
extern NSString *kRemoteControlOtherTapped;
@interface UIApplication (RemoteControl)
/**
* 注册对remote control事件的监听
*
* @param observer 监听者
* @param selector 回调
*/
- (void)observeRemoteControl:(id)observer selector:(SEL)selector;
@end
//
// UIApplication+RemoteControl.m
// IOSAudioRemoteControl
//
// Created by huangyibiao on 15/3/25.
// Copyright (c) 2015年 huangyibiao. All rights reserved.
//
#import "UIApplication+RemoteControl.h"
const NSString *kRemoteControlPlayTapped = @"kRemoteControlPlayTapped";
const NSString *kRemoteControlPauseTapped = @"kRemoteControlPauseTapped";
const NSString *kRemoteControlStopTapped = @"kRemoteControlStopTapped";
const NSString *kRemoteControlPreviousTapped = @"kRemoteControlForwardTapped";
const NSString *kRemoteControlNextTapped = @"kRemoteControlBackwardTapped";
const NSString *kRemoteControlOtherTapped = @"kRemoteControlOtherTapped";
@implementation UIApplication (RemoteControl)
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self postNotification:kRemoteControlPlayTapped];
break;
case UIEventSubtypeRemoteControlPause:
[self postNotification:kRemoteControlPauseTapped];
break;
case UIEventSubtypeRemoteControlStop:
[self postNotification:kRemoteControlStopTapped];
break;
case UIEventSubtypeRemoteControlNextTrack:
[self postNotification:kRemoteControlNextTapped];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[self postNotification:kRemoteControlPreviousTapped];
break;
default:
[self postNotification:kRemoteControlOtherTapped];
break;
}
}
- (void)postNotification:(const NSString *)notificationName {
[[NSNotificationCenter defaultCenter]
postNotificationName:(NSString *)notificationName object:nil];
}
- (void)observeRemoteControl:(id)observer selector:(SEL)selector {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:observer selector:selector name:(NSString *)kRemoteControlNextTapped object:nil];
[center addObserver:observer selector:selector name:(NSString *)kRemoteControlPauseTapped object:nil];
[center addObserver:observer selector:selector name:(NSString *)kRemoteControlStopTapped object:nil];
[center addObserver:observer selector:selector name:(NSString *)kRemoteControlPreviousTapped object:nil];
[center addObserver:observer selector:selector name:(NSString *)kRemoteControlPlayTapped object:nil];
[center addObserver:observer selector:selector name:(NSString *)kRemoteControlOtherTapped object:nil];
}
@end
下面来测试一下:
#import "ViewController.h"
#import <MediaPlayer/MPNowPlayingInfoCenter.h>
#import "UIApplication+RemoteControl.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController () {
UITextView *_textView;
AVPlayer *_player;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height - 40)];
_textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
_textView.textAlignment = NSTextAlignmentLeft;
_textView.textColor = [UIColor redColor];
_textView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_textView];
_textView.editable = NO;
_textView.text = @"操作remote control,会在这里显示出来所执行的操作记录。。。\n";
[[UIApplication sharedApplication] observeRemoteControl:self
selector:@selector(onRemoteControlNotification:)];
_player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://stream.jewishmusicstream.com:8000"]];
[_player play];
}
- (void)onRemoteControlNotification:(NSNotification *)notification {
if ([notification.name isEqualToString:kRemoteControlPlayTapped]) {
[self showLog:@"remote control play event"];
[_player play];
} else if ([notification.name isEqualToString:kRemoteControlPauseTapped]) {
[self showLog:@"remote contrl pause event"];
[_player pause];
} else if ([notification.name isEqualToString:kRemoteControlNextTapped]) {
[self showLog:@"remote control play next event"];
} else if ([notification.name isEqualToString:kRemoteControlPreviousTapped]) {
[self showLog:@"remote control play previous event"];
} else if ([notification.name isEqualToString:kRemoteControlStopTapped]) {
[self showLog:@"remote control stop event"];
[_player pause];
} else if ([notification.name isEqualToString:kRemoteControlOtherTapped]) {
[self showLog:@"remote control other event"];
}
}
- (void)showLog:(NSString *)text {
_textView.text = [NSString stringWithFormat:@"%@\n%@", _textView.text, text];
}
@end
获取Demo: https://github.com/632840804/IOSAudioRemoteControl