我将Spotify播放器添加到我的应用程序中,该应用程序也使用MPMusicPlayerController播放音乐。当Spotify播放音乐并且屏幕被锁定时,当用户在锁定屏幕上按下这些按钮时,不会收到播放/暂停和FFW / RWD的遥控器事件。
如果从MPMusicPlayerController播放音乐,我可以根据以下代码接收遥控器事件:
-(void) ViewDidLoad {
...
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
...
}
和
- (BOOL) canBecomeFirstResponder
{
return YES;
}
- (void) remoteControlReceivedWithEvent: (UIEvent*) event
{
// see [event subtype] for details
if (event.type == UIEventTypeRemoteControl) {
// We may be receiving an event from the lockscreen
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
// User pressed play or pause from lockscreen
[self playOrPauseMusic:nil];
break;
case UIEventSubtypeRemoteControlNextTrack:
// User pressed FFW from lockscreen
[self fastForwardMusic:nil];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
// User pressed rewind from lockscreen
[self rewindMusic:nil];
break;
default:
break;
}
}
}
当应用程序进入后台时,iPod控件可见,但当我按暂停时,它们不会响应。相反,当我按下暂停时iPod控制消失。当从锁定屏幕在后台播放Spotify等流式传输音频时,需要增加什么才能检测播放/暂停和FFW / RWD?
答案 0 :(得分:2)
我相信我过去遇到过这种情况。如果我没记错的话,我在
中添加了-(void)remoteControlReceivedWithEvent:(UIEvent *) event { ... }
以及
- (BOOL) canBecomeFirstResponder { return YES; }
到app delegate(这也是我的音频控制器所在的地方)。在我想要捕获UIEventTypeRemoteControl
通知期间,我遇到了UIViewControllers未处于活动状态的问题。
尝试一下,看看是否有帮助。
答案 1 :(得分:0)
经过进一步调查后,我发现如果我的应用程序进入后台并且收到遥控器事件时包含以下代码,则iPod控件不会消失。
// Set up info center to display album artwork within ipod controls (needed for spotify)
MPMediaItemArtwork *ipodControlArtwork = [[MPMediaItemArtwork alloc]initWithImage:artworkImage];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys:nowPlayingTitle, MPMediaItemPropertyTitle,
nowPlayingArtist, MPMediaItemPropertyArtist, ipodControlArtwork, MPMediaItemPropertyArtwork, [NSNumber numberWithDouble:0.0], MPNowPlayingInfoPropertyPlaybackRate, nil];