2017-10-10 10 views
0

에 전화했을 때 내가 연주 미디어신속한 업데이트 미디어 메타 데이터는 두 가지 방법

을위한 미디어 CC/잠금 화면

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 
       ] 
} 

의 정보와 기능을 표시 해야하는 두 가지 방법이 작동하지 않습니다

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() 
} 

미디어 파일이 변경되었을 때만 초당 재생률과 일반 메타 데이터를 업데이트하고 싶습니다. 두 가지 기능이 모두있는 경우 LS/CC에 재생 시간 막대가 없으므로 updateGeneralData() 만 작동하는 것처럼 보입니다 제목, 아티스트, 앨범 제목, 아트 워크, 현재 시간, 비율 및 재생 시간과 같은 모든 정보를 updatePlaybackRateData()가 모든 것을 보여 주지만 필요한 정보 만 매 초마다 업데이트되므로 두 가지 메소드간에 해당 기능을 분할하고 싶습니다.

답변

0

분명히 아래의 해결책이 문제를 해결합니다. 나는 새로운 일정

let infoCC = MPNowPlayingInfoCenter.default()

을 생성에 방법을 변경했습니다 : 잘하면 미래

에있는 사람을 도움이

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 
}