如何更改锁定屏幕/控制中心的轨道位置?

时间:2013-10-25 13:10:02

标签: ios ios7 remote-control control-center

使用ios 7音乐应用程序播放歌曲时,用户可以使用滑块更改锁定屏幕/控制中心中的歌曲位置。滑块处于活动状态:

enter image description here

但是在我的应用中播放音乐的用户无法做到这一点。滑块未激活:

enter image description here

如何在我的应用中启用这些功能?

3 个答案:

答案 0 :(得分:12)

您可以借助iOS 9.1及更高版本上的MPRemoteCommandCenter更改曲目位置。

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_0) {
            MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
            [commandCenter.changePlaybackPositionCommand setEnabled:true];
            [commandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changedThumbSliderOnLockScreen:)];
        }

和方法

- (MPRemoteCommandHandlerStatus)changedThumbSliderOnLockScreen:(MPChangePlaybackPositionCommandEvent *)event
{
    // change position
    [self setCurrentPlaybackTime:event.positionTime];
    // update MPNowPlayingInfoPropertyElapsedPlaybackTime
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

    return MPRemoteCommandHandlerStatusSuccess;
}

答案 1 :(得分:4)

我一直在寻找相同的东西,但我认为这不可能看到这篇文章:

How to enable audio scrubber in iOS Lock Screen control panel?

Spotify和Soundcloud等热门应用也没有实现这一点。

如果您正在寻找在锁定屏幕上显示当前音乐的方法,则需要执行以下操作。

首次播放新曲目时,请更新NowPlayingInfo:

NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

    [songInfo setObject:trackTitle forKey:MPMediaItemPropertyTitle];
    [songInfo setObject:artistName forKey:MPMediaItemPropertyArtist];
    [songInfo setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
    [songInfo setObject:releaseDate forKey:MPMediaItemPropertyReleaseDate];
    [songInfo setValue:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];
    [songInfo setObject:elapsedTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    [songInfo setObject:albumArtImage forKey:MPMediaItemPropertyArtwork];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

要处理来自锁屏的事件,首先需要告诉您的应用开始从遥控器接收事件。我在AppDelegate的应用程序didFinishLaunchingWithOptions中使用以下代码执行此操作

 // Turn on remote control event delivery
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

接下来,您需要实现remoteControlReceivedWithEvent方法来处理捕获的事件。在APPDelegate中添加以下方法

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {

 if (receivedEvent.type == UIEventTypeRemoteControl) {

    switch (receivedEvent.subtype) {
        case UIEventSubtypeRemoteControlPause:
            //pause code here
            break;

        case UIEventSubtypeRemoteControlPlay:
             //play code here
            break;

        case UIEventSubtypeRemoteControlPreviousTrack:
            // previous track code here
            break;

        case UIEventSubtypeRemoteControlNextTrack:
            //next track code here
            break;

        default:
            break;
    }
 }

}

有关来自apple docs的MPNowPlayingInfoCenter的更多信息 - > https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPNowPlayingInfoCenter_Class

答案 2 :(得分:4)

<强> swift4 您可以借助iOS 9.1及更高版本上的MPRemoteCommandCenter更改曲目位置。

    let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.changePlaybackPositionCommand.isEnabled = true
        commandCenter.changePlaybackPositionCommand.addTarget(self, action:#selector(changePlaybackPositionCommand(_:)))

和方法

    @objc func changePlaybackPositionCommand(_ event:MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus{
let time = event.positionTime
        //use time to update your track time
        return MPRemoteCommandHandlerStatus.success;
    }

请注意,如果要启用commandCenter中的每个命令,则必须执行此操作