加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3 – 锁屏iPhone的音乐控制

发布时间:2020-12-14 04:40:24 所属栏目:百科 来源:网络整理
导读:我正在开发音乐应用程序,但对于锁定屏幕控制,我无法分配持续时间和已用时间 这是我的代码 let commandCenter = MPRemoteCommandCenter.shared() commandCenter.previousTrackCommand.isEnabled = true; commandCenter.previousTrackCommand.addTarget(self,a
我正在开发音乐应用程序,但对于锁定屏幕控制,我无法分配持续时间和已用时间

这是我的代码

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进行编码

解决方法

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

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

> MPNowPlayingInfoPropertyElapsedPlaybackTime,所以它知道你开始玩的时候是什么时候,
> MPMediaItemPropertyPlaybackDuration,所以它知道当前时间相对于酒吧是什么,并且
> MPNowPlayingInfoPropertyPlaybackRate,因此它可以自动更新播放时间UI,而无需您定期设置当前时间.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读