你没有看错,这里说的运动事件,只是摇晃一下手机而已,所以只有运动开始、运动结束、运动取消,无法取得运动过程中的运动速度、运动方向等数据,这些需要另外的框架去实现,我们可以理解这里的运动时间为 “摆动事件” 。
#import "ViewController.h"
@interface ViewController (){
UIButton *_playButton;
BOOL _isPlaying;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self initLayout];
}
- (BOOL)canBecomeFirstResponder{
return NO;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSURL *url = [NSURL URLWithString:@"
http://stream.jewishmusicstream.com:8000"];
_player = [[AVPlayer alloc] initWithURL:url];
}
#pragma mark 远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{
if(event.type == UIEventTypeRemoteControl){
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[_player play];
_isPlaying = true;
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
[self btnClick:_playButton];
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"Next...");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"Previous...");
break;
case UIEventSubtypeRemoteControlBeginSeekingForward:
NSLog(@"Begin seek forward...");
break;
case UIEventSubtypeRemoteControlEndSeekingForward:
NSLog(@"End seek forward...");
break;
case UIEventSubtypeRemoteControlBeginSeekingBackward:
NSLog(@"Begin seek backward...");
break;
case UIEventSubtypeRemoteControlEndSeekingBackward:
NSLog(@"End seek backward...");
break;
default:
break;
}
[self changeUIState];
}
}
#pragma mark 界面布局
- (void)initLayout{
//专辑封面
UIImage *image = [UIImage imageNamed:@"wxl.jpg"];
CGRect *frame = [UIScreen mainScreen].applicationFrame;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = image;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView];
//播放控制面板
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 88)];
view.backgroundColor = [UIColor lightGrayColor];
view.alpha = 0.9;
[self.view addSubview:view];
//添加播放按钮
_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
_playButton.bounds = CGRectMake(0, 0, 50, 50);
CGFloat playBtnX = view.frame.size.width/2;
CGFloat playBtnY = view.frame.size.height/2;
_playButton.center = CGPointMake(playBtnX, playBtnY);
[self changeUIState];
[_playButton addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:_playButton];
}
#pragma mark 界面状态
- (void)changeUIState{
if(_isPlaying){
UIImage *pauseImage = [UIImage imageNamed:@"playing_btn_pause_n.png"];
UIImage *pauseImageH = [UIImage imageNamed:@"playing_btn_pause_h.png"];
[_playButton setImage:pauseImage forState:UIControlStateNormal];
[_playButton setImage:pauseImageH forState:UIControlStateHighlighted];
}else{
UIImage *playImage = [UIImage imageNamed:@"playing_btn_play_n.png"];
UIImage *playImageH = [UIImage imageNamed:@"playing_btn_play_h.png"];
[_playButton setImage:playImage forState:UIControlStateNormal];
[_playButton setImage:playImageH forState:UIControlStateHighlighted];
}
}
- (void)btnClick:(UIButton *)btn{
if (_isPlaying) {
[_player pause];
}else{
[_player play];
}
_isPlaying =! _isPlaying;
[self changeUIState];
}
@end