如果您想让您的app可以像ipod一样在后台也可以方便的通过双击HOME键来控制,就要用到远程控制事件了。
首先在viewdidload等初始化的地方声明App接收远程控制事件,并在相应地方结束声明
- - (void) viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [UIApplication sharedApplication] beginReceivingRemoteControlEvents];
- [self becomeFirstResponder];
- }
- - (void) viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [UIApplication sharedApplication] endReceivingRemoteControlEvents];
- [self resignFirstResponder];
- }
- - (BOOL)canBecomeFirstResponder
- {
- return YES;
- }
- - (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
- if (receivedEvent.type == UIEventTypeRemoteControl) {
- switch (receivedEvent.subtype) {
- case UIEventSubtypeRemoteControlTogglePlayPause:
- [self playButtonPressed:playButton];
- [self testing];
- break;
- case UIEventSubtypeRemoteControlPreviousTrack:
- [self rewButtonReleased:(UIButton *)rewButton];
- break;
- case UIEventSubtypeRemoteControlNextTrack:
- [self ffwButtonReleased:(UIButton *)ffwButton];
- break;
- default:
- break;
- }
- }
- }
4. 至此,您有播放App已经相当完美了,还有最后一个问题,那就是当用户使用耳机时,问题又来了。系统默认当插入耳机时,正在播放的声音不中断,直接切换到耳机播放,而当拔出耳机时,播放停止。如果这种行为满足您的要求,那OK,否则您就需要进一步研究耳机检测和声音路由切换的问题。
