首先,您必须设置 nowPlaying 元数据,并在您更改任何内容时调用它。
//MARK: setupNowPlaying----------------------------------
func setupNowPlaying() {
// Define Now Playing Info
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = self.nowPlayingString
let image = UIImage(named: "Somni-lockLogo")! // this is the image you want to see on the lock screen
let artwork = MPMediaItemArtwork.init(boundsSize: image.size,
requestHandler: { (size) -> UIImage in
return image
})
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = self.player.currentTime
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = self.player.duration
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
//MARK: now playing
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
nowPlayingInfo[MPMediaItemPropertyArtist ] = self.nowPlayingTitle
// other metadata exists, check the documentation
// nowPlayingInfo[MPMediaItemPropertyArtist] = "David Bowie"
// nowPlayingInfo[MPMediaItemPropertyComposer] = "Bill Gates"
// Set the metadata
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
然后你需要设置远程传输控件,启用你需要的东西,比如播放暂停跳过等这是我的功能的开始,它启用东西并包含代码以使洗涤器工作
func setupRemoteTransportControls() {
// Get the shared MPRemoteCommandCenter
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.pauseCommand.isEnabled = true
let skipBackwardIntervalCommand: MPSkipIntervalCommand? = commandCenter.skipBackwardCommand
let skipForwardIntervalCommand: MPSkipIntervalCommand? = commandCenter.skipForwardCommand
let seekForwardCommand: MPRemoteCommand? = commandCenter.seekForwardCommand
let seekBackwardCommand: MPRemoteCommand? = commandCenter.seekBackwardCommand
seekForwardCommand?.isEnabled = true
seekBackwardCommand?.isEnabled = true
skipBackwardIntervalCommand!.isEnabled = true
skipForwardIntervalCommand!.preferredIntervals = [60]
skipBackwardIntervalCommand!.preferredIntervals = [60]
commandCenter.changePlaybackPositionCommand.isEnabled = true
commandCenter.changePlaybackPositionCommand.addTarget
{ (event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus in
let event = event as! MPChangePlaybackPositionCommandEvent
print("change playback",event.positionTime)
self.player.currentTime = event.positionTime
self.setupNowPlaying()
return .success
}
// etc etc 无论你想使用什么都需要一个处理程序,你需要在处理程序中将事件类型设置为正确的类型。