tvOS nowPlayingInfo not showing - SOLVED

iOS, iPad, iPhone, tvOS specific usage questions
shaybc
Blank Cone
Blank Cone
Posts: 39
Joined: 27 Mar 2016 13:59

tvOS nowPlayingInfo not showing - SOLVED

Postby shaybc » 01 Oct 2021 07:53

trying to display played media info on the apple tv "Notification Center" area:

Image

using the nowPlayingInfo dictionary (i am using SwiftUI), but any working code sample would do, this is a sample code i found, but it's not working for me:

Code: Select all

let center = MPNowPlayingInfoCenter.default() center.nowPlayingInfo = [ MPMediaItemPropertyTitle: "Mastering UIKit on tvOS", MPMediaItemPropertyArtist: "Apple", MPMediaItemPropertyAlbumTitle: "WWDC 2016", MPMediaItemPropertyMediaType: MPNowPlayingInfoMediaType.video.rawValue, MPNowPlayingPlaybackState: MediaState MPNowPlayingInfoPropertyElapsedPlaybackTime: 0.0, MPMediaItemPropertyPlaybackDuration: 3300.0 ]
(seen on: https://docplayer.net/61615774-Now-play ... -tvos.html)

i tried yo put this code in the "playerTimeChanged" callback and in the player "onAppear" none of it worked

any ideas?
Last edited by shaybc on 01 Oct 2021 11:22, edited 1 time in total.

shaybc
Blank Cone
Blank Cone
Posts: 39
Joined: 27 Mar 2016 13:59

Re: tvOS nowPlayingInfo not showing - SOLVED

Postby shaybc » 01 Oct 2021 11:18

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 }


Return to “VLC for iOS, iPadOS and Apple TV”

Who is online

Users browsing this forum: No registered users and 9 guests