Fixe the now playing info center while a voice broadcast is played

This commit is contained in:
Nicolas Mauri
2023-01-10 16:57:13 +01:00
parent cb7771916b
commit fefa22cfe7
5 changed files with 59 additions and 4 deletions
@@ -16,6 +16,7 @@
import Combine
import SwiftUI
import MediaPlayer
// TODO: VoiceBroadcastPlaybackViewModel must be revisited in order to not depend on MatrixSDK
// We need a VoiceBroadcastPlaybackServiceProtocol and VoiceBroadcastAggregatorProtocol
@@ -302,6 +303,7 @@ class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, Voic
// Init and start the player on the first chunk
let audioPlayer = self.mediaServiceProvider.audioPlayerForIdentifier(result.eventIdentifier)
audioPlayer.registerDelegate(self)
self.mediaServiceProvider.setNowPlayingInfoProvider(self, forPlayer: audioPlayer)
audioPlayer.loadContentFromURL(result.url, displayName: chunk.attachment.originalFileName)
self.audioPlayer = audioPlayer
@@ -482,3 +484,19 @@ extension VoiceBroadcastPlaybackViewModel: VoiceMessageAudioPlayerDelegate {
stopIfVoiceBroadcastOver()
}
}
// MARK: - NowPlayingInfoProvider
extension VoiceBroadcastPlaybackViewModel: VoiceMessageNowPlayingInfoProvider {
func updatePlayingInfoCenter(forPlayer player: VoiceMessageAudioPlayer) {
guard audioPlayer != nil, audioPlayer === player else {
return
}
let nowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
nowPlayingInfoCenter.nowPlayingInfo = [MPMediaItemPropertyTitle: VectorL10n.voiceBroadcastPlaybackLockScreenPlaceholder,
MPMediaItemPropertyPlaybackDuration: (state.playingState.duration / 1000.0) as Any,
MPNowPlayingInfoPropertyElapsedPlaybackTime: (state.bindings.progress / 1000.0) as Any,
MPNowPlayingInfoPropertyPlaybackRate: state.playbackState == .playing ? 1 : 0]
}
}