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

问题描述 投票:8回答:3

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

但是当我在我的应用程序中播放音乐时,用户无法做到。滑块未激活:

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

ios ios7 remote-control control-center
3个回答
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;
}

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

要处理来自Lockscreen的事件,首先需要告诉您的应用程序开始从遥控器接收事件。我在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;
    }
 }

}

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


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中的每个命令执行此操作

© www.soinside.com 2019 - 2024. All rights reserved.