首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在两个方法中调用时,Swift在Control Center中更新媒体元数据不起作用

在两个方法中调用时,Swift在Control Center中更新媒体元数据不起作用
EN

Stack Overflow用户
提问于 2017-10-10 18:02:17
回答 1查看 736关注 0票数 0

我有两种方法可以在CC/Lock screen中显示媒体信息

代码语言:javascript
复制
func updateGeneralMetadata() {
        guard player.url != nil, let _ = player.url else {
            nowPlayingInfoCenter.nowPlayingInfo = nil
            return
        }
        let item = currentItem
        var rating = ""
        for _ in 0 ..< item!.rating{
            rating.append("*")
        }
        if item?.assetURL != nil{
            MPNowPlayingInfoCenter.default().nowPlayingInfo = [
                MPMediaItemPropertyTitle: item?.value(forProperty: MPMediaItemPropertyTitle)!,
                MPMediaItemPropertyArtist: item?.value(forProperty: MPMediaItemPropertyArtist)!,
                MPMediaItemPropertyAlbumTitle: rating,
                MPMediaItemPropertyArtwork: item?.artwork ?? UIImage()
            ]
        }
    }

func updatePlaybackRateData(){
        guard currentItem?.assetURL != nil else {
            duration = 0
            nowPlayingInfoCenter.nowPlayingInfo = nil
            return
        }
        duration = Float(player.duration)
        let item = currentItem
        MPNowPlayingInfoCenter.default().nowPlayingInfo = [
                    MPNowPlayingInfoPropertyElapsedPlaybackTime: player.currentTime,
                    MPNowPlayingInfoPropertyPlaybackRate: player.rate,
                    MPMediaItemPropertyPlaybackDuration: item?.playbackDuration
                ]
}

和播放媒体的功能

代码语言:javascript
复制
func play(){
player.play()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(Plum.updatePlaybackRateData), userInfo: nil, repeats: true)
        timer.fire()
        NotificationCenter.default.post(name: Plum.playBackStateChanged, object: nil, userInfo: ["Artist": "Title"])
        updateGeneralMetadata()
}

如你所见,我希望每秒更新一次播放速率,并且仅在媒体文件发生更改时更新一般元数据。当我有像那样的两个函数时,似乎只有updateGeneralData()有效,因为当我把所有的信息,如标题,艺术家,albumTitle,艺术作品,currentTime,速率和持续时间都显示在updatePlaybackRateData()中时,LS/CC上没有回放时间条,但我想在两个方法之间拆分这种功能性,以便只有必要的信息每秒更新一次。

EN

回答 1

Stack Overflow用户

发布于 2017-10-10 19:15:38

显然,下面的解决方案解决了这个问题。我创建了一个新的常量

let infoCC = MPNowPlayingInfoCenter.default()

并将方法更改为:

代码语言:javascript
复制
func updateGeneralMetadata() {
        guard player.url != nil, let _ = player.url else {
            infoCC.nowPlayingInfo = nil
            return
        }
        let item = currentItem
        var nowPlayingInfo = infoCC.nowPlayingInfo ?? [String: Any]()
        nowPlayingInfo[MPMediaItemPropertyTitle] = item?.title
        nowPlayingInfo[MPMediaItemPropertyArtist] = item?.albumArtist
        nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = item?.albumTitle
        nowPlayingInfo[MPMediaItemPropertyArtwork] = item?.artwork
        infoCC.nowPlayingInfo = nowPlayingInfo
    }

    func updatePlaybackRateData(){
        guard currentItem?.assetURL != nil else {
            duration = 0
            infoCC.nowPlayingInfo = nil
            return
        }
        var nowPlayingInfo = infoCC.nowPlayingInfo ?? [String: Any]()
        duration = Float(player.duration)
        let item = currentItem
        nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime
        nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
        nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = player.duration
        infoCC.nowPlayingInfo = nowPlayingInfo
}

希望它能在未来帮助某些人

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46663944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档