锁屏iPhone的音乐控制

时间:2016-11-03 15:15:13

标签: swift3 lockscreen mpnowplayinginfocenter

我正在开发音乐应用程序,但对于锁定屏幕控制,我无法指定持续时间和已用时间

这是我的代码

let commandCenter = MPRemoteCommandCenter.shared()

        commandCenter.previousTrackCommand.isEnabled = true;
        commandCenter.previousTrackCommand.addTarget(self, action:#selector(home_ViewController.btn_rewind(_:)))

        commandCenter.nextTrackCommand.isEnabled = true
        commandCenter.nextTrackCommand.addTarget(self, action:#selector(home_ViewController.btn_fast(_:)))

        commandCenter.playCommand.isEnabled = true
        commandCenter.playCommand.addTarget(self, action:#selector(home_ViewController.play_player))

        commandCenter.pauseCommand.isEnabled = true
        commandCenter.pauseCommand.addTarget(self, action:#selector(home_ViewController.pause_player))

        commandCenter.togglePlayPauseCommand.isEnabled = true
        commandCenter.togglePlayPauseCommand.addTarget(self, action:#selector(home_ViewController.togglePlay_Pause))

        commandCenter.skipBackwardCommand.isEnabled = false
        commandCenter.skipForwardCommand.isEnabled = false

        if #available(iOS 9.1, *) {
            commandCenter.changePlaybackPositionCommand.isEnabled = true
        } else {
            // Fallback on earlier versions
            return
        }

和媒体信息

func setLockInfo()
    {
        let url = URL(string: song_UrlString)
        let data = try? Data(contentsOf: url!)
        let art = MPMediaItemArtwork.init(image: UIImage(data: data!)!)



        let songInfo :[String : Any] = [MPMediaItemPropertyTitle :st_title,MPMediaItemPropertyArtwork : art]

        MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo


    }

我正在获取标题和图片,但锁定屏幕没有显示时间

我使用SWIFT 3代码

2 个答案:

答案 0 :(得分:4)

它没有显示时间,因为你没有告诉它显示时间。

要显示播放时间,您的nowPlayingInfo字典需要包含键的值:

如果您希望播放时间栏是交互式的(即允许跳转到不同的时间而不是仅显示当前时间,请在远程命令中心注册changePlaybackPositionCommand

答案 1 :(得分:0)

Swift 3 示例包含所有命令,包括 changePlaybackPositionCommand

func remotePlayerInit() {
    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()

    commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseSongTouch(_:)))
    commandCenter.playCommand.addTarget(self, action: #selector(self.playSongTouch(_:)))
    commandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextSongTouch(_:)))
    commandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousSongTouch(_:)))
    commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(self.changedThumbSlider(_:)))

    setLockInfo()
}

func changedThumbSlider(_ event: MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus {
    audioPlayer.currentTime = event.positionTime
    setLockInfo()
    return .success
}

func setLockInfo()
{
    let image = PlayerVC.songs[PlayerVC.currentSelection].image
    let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
        return image
    })

    let songInfo: [String: Any] = [MPMediaItemPropertyTitle:  PlayerVC.songs[PlayerVC.currentSelection].name, 
                                   MPMediaItemPropertyArtwork: artwork, 
                                   MPNowPlayingInfoPropertyElapsedPlaybackTime: TimeInterval(audioPlayer.currentTime),
                                   MPNowPlayingInfoPropertyPlaybackRate: 1,
                                   MPMediaItemPropertyPlaybackDuration: audioPlayer.duration,
                                   MPMediaItemPropertyArtist: "Artist"]

    MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo
}