Found my problem - you need to populate the NowPlaying info
!! BEFORE !! you start playing the media ( mediaPlayer.play() ) and add RemoteCommandCenter play/pause actions (not sure why the one related to the other),
maybe apple tv 'listen' to the play event and only then it understands that the media started to play and display the info on the NowPlayingInfoCenter and on my iPhone etc...
that means setting the MPNowPlayingInfoPropertyElapsedPlaybackTime (on the "playerTimeChanged" callback) had no effect,
(although this answer:
https://stackoverflow.com/a/43334461/530884)
and also setting the MPNowPlayingInfoPropertyPlaybackRate to 1.0 (playing speed) - had no effect as well
only after i paused and pressed play again it started working - that made me realize that it needs to be set before the first play
so to add the RemoteCommandCenter actions i added this code after the NowPlayingInfoCenter section:
Code: Select all
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.pauseCommand.isEnabled = true
commandCenter.playCommand.addTarget { [self] (commandEvent) -> MPRemoteCommandHandlerStatus in
mediaPlayer.play()
return MPRemoteCommandHandlerStatus.success
}
commandCenter.pauseCommand.addTarget { [self] (commandEvent) -> MPRemoteCommandHandlerStatus in
mediaPlayer.pause()
return MPRemoteCommandHandlerStatus.success
}
and that did the trick, so the fully code should look something like this:
Code: Select all
// get the NowPlaying InfoCenter default reference
let mpNowPlayingInfoCenter = MPNowPlayingInfoCenter.default()
// prepare video / audio image as UIImage object
let artwork = MPMediaItemArtwork(image: UIImage(systemName: "star.fill")!)
mpNowPlayingInfoCenter.nowPlayingInfo = [
MPMediaItemPropertyTitle: "Video Name",
MPMediaItemPropertyArtist: "Artist Name",
MPMediaItemPropertyAlbumTitle: "Album Title",
MPMediaItemPropertyArtwork: artwork,
MPMediaItemPropertyMediaType: MPNowPlayingInfoMediaType.video.rawValue, // can also be audio
MPNowPlayingInfoPropertyElapsedPlaybackTime: 0.0, // just starting
MPNowPlayingInfoPropertyPlaybackRate: 1.0, // this indicates the playing speed
MPMediaItemPropertyPlaybackDuration: 3300.0 // 55 minutes as an example for video length
]
// set media details to NowPlaying Info center (this is just for MacOS but i tried it anyway)
mpNowPlayingInfoCenter.playbackState = MPNowPlayingPlaybackState.playing
// get the remote command center shared instance
let commandCenter = MPRemoteCommandCenter.shared()
// declare that this app want to receive play and pause actions from the Apple-Tv InfoCenter & iphone NowPlaying widget
commandCenter.playCommand.isEnabled = true
commandCenter.pauseCommand.isEnabled = true
// set what callback action would take place when play press event is received from the Apple-Tv InfoCenter & iphone NowPlaying widget
commandCenter.playCommand.addTarget { [self] (commandEvent) -> MPRemoteCommandHandlerStatus in
mediaPlayer.play()
return MPRemoteCommandHandlerStatus.success
}
// set what callback action would take place when pause press event is received from the Apple-Tv InfoCenter & iphone NowPlaying widget
commandCenter.pauseCommand.addTarget { [self] (commandEvent) -> MPRemoteCommandHandlerStatus in
mediaPlayer.pause()
return MPRemoteCommandHandlerStatus.success
}