锁定屏幕上的命令中心洗涤器迅速

时间:2018-07-11 14:55:14

标签: swift xcode mpremotecommandcenter

我正在尝试将洗涤器添加到命令中心锁定屏幕,但出现此错误无法分配给值:函数调用返回不可变的值,我不知道这意味着什么。任何帮助将不胜感激。

这就是我尝试更改职位的方式

commandCenter.changePlaybackPositionCommand.addTarget(handler: {
        (event) in
        let event = event as! MPChangePlaybackPositionCommandEvent
        self.player.currentTime() = event.positionTime  // ERROR
        return MPRemoteCommandHandlerStatus.success
    })

2 个答案:

答案 0 :(得分:0)

我认为您的播放器属性是AVPlayer(???),如果是这样,您想使用seek函数设置currentTime,而不是从函数中设置返回值...

self.player.seek(to: CMTimeMakeWithSeconds(event.positionTime, 1000000))

答案 1 :(得分:0)

首先,您必须设置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
        }

//等,无论您想使用什么,都需要一个处理程序,并且需要在处理程序中将事件类型设置为正确的类型。