我一直在寻找同样的东西,但我认为这是不可能的,请参阅这篇文章:
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;
}
}
}
苹果文档中有关 MPNowPlayingInfoCenter 的更多信息 -> https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPNowPlayingInfoCenter_Class