锁屏iPod控件无法与Spotify音乐播放器一起使用 [英] Lock Screen iPod Controls Not Working With Spotify Music Player

查看:177
本文介绍了锁屏iPod控件无法与Spotify音乐播放器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spotify播放器添加到了我的应用中,该播放器还使用MPMusicPlayerController播放音乐.从Spotify播放音乐并且锁定屏幕时,当用户在锁定的屏幕上按下这些按钮时,不会收到播放/暂停和FFW/RWD的远程控制事件.

I added the Spotify player to my app which also plays music using the MPMusicPlayerController. When music is playing from Spotify and the screen is locked, the remote control events are not received for play/pause and FFW/RWD when the user presses these buttons on the locked screen.

如果正在从MPMusicPlayerController播放音乐,则可以基于以下代码接收远程控制事件:

If music is playing from the MPMusicPlayerController, I am able to receive the remote control events based on the following code:

-(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?

While the iPod controls are visible when the app enters the background, they do not respond when I press pause. Instead, the iPod controls disappear when I press pause. What addition is needed to enable detection of play/pause and FFW/RWD when streaming audio such as Spotify is playing in the background from lock screen?

推荐答案

经过进一步调查,我发现如果在我的应用程序进入后台并接收到遥控器事件时,如果包含以下代码,则iPod控件不会消失.

After further investigation, I have found that if include the following code when my app enters the background and when the remote control events are received, the iPod controls do not disappear.

// 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];

这篇关于锁屏iPod控件无法与Spotify音乐播放器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆