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

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

在这里输入图像说明

但是,当我的应用程序用户播放音乐无法做到这一点。 滑块不活动:

在这里输入图像说明

如何在我的应用程序中启用这些function?

您可以在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; } 

我正在寻找同样的东西,但我不认为这是可能的看到这个职位:

如何在iOS锁屏控制面板中启用audio清理程序?

Spotify和Soundcloud等stream行应用程序也没有实现。

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

首先当您播放一个新的轨道更新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从苹果文档 – > https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPNowPlayingInfoCenter_Class