Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: now playing metadata changed hook and event #2250

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tomislav-arambasic
Copy link

@tomislav-arambasic tomislav-arambasic commented Feb 6, 2024

Summary

Resolves #2158.

Added nowPlayingMetadata hook, getNowPlayingMetadata getter and NowPlayingMetadataChanged event.
Reflect these changes in updateMetadataForTrack (when it's the active track) and clearNowPlayingMetadata for time before deprecation.

useActiveTrack() stays intact, as David suggested in issue, I've done changes as per his recommendations, implementing new event, getter and hook, independently of useActiveTrack.

  • Users had no way of obtaining now playing metadata before, for metadata rendering purposes
    • activeTrack event, getter and hook do not provide now playing metadata information - if users would ever change
      metadata of track that is currently playing, there would be no update to it
    • With radio streams, when rendering now playing metadata artist, title and artwork... Radio screen metadata UI can be updated once MetadataTimedReceived event is emitted. But, if user would unmount the screen and then come back to it, now playing metadata is not accessible, until new MetadataTimedReceived event is emitted - when song on the radio stream changes

How it works

  • Every time updateNowPlayingMetadata, or updateMetadataForTrack, if it's active track, is called, new metadata is saved into private var inside MusicService / RNTrackPlayer and NowPlayingMetadataChanged event is emitted. clearNowPlayingMetadata reflects these changes accordingly, until it'll be deprecated as announced
  • getNowPlayingMetadata returns that variable's value, last stored metadata
  • useNowPlayingMetadata fetches that variable's value on mount, saves it into state. Every time NowPlayingMetadataChanged is emitted, it updates the internal state with new metadata. Hook returns metadata
  • NowPlayingMetadataChanged is subscribable, event returns metadata object

Testing

  • Tested in Example app, using iPhone 13 mini (iOS 17.2) and Google Pixel 3 (Android 12)
  • Tested in my app containing podcast RSS feeds and radio streams, using iPhone 13 mini (iOS 17.2) and Google Pixel 3 (Android 12)

Screenshots

Note that artwork gets updated randomly, and because notification metadata and our nowPlayingMetadata update at at different time, they're not the same.

Now playing metadata updated on "Update Notification Metadata Randomly" action Now playing metadata updated on "Update Current Track Metadata Randomly" action
iOS
Android
Now playing metadata updated for each track via PlaybackService
iOS
Android

Note

@dcvz
I've just noticed that notification metadata is "patched", instead of replaced.
E.g., if we update now playing metadata, or metadata for active track, it'll keep old metadata values for any undefined values in object we're updating with.
I'm not sure if this should be the case, I'd expect users to be able to override notification data completely.
nowPlayingMetadata is replaced completely with new values, even if they're undefined.

@tomislav-arambasic tomislav-arambasic changed the title Feat/now playing metadata changed hook and event feat: now playing metadata changed hook and event Feb 6, 2024
@dcvz
Copy link
Contributor

dcvz commented Feb 7, 2024

Thanks for taking the time @tomislav-arambasic ! As you saw there's been some conversation on the other thread around options and possibilities.

I'll review this PR later this week!

@dcvz
Copy link
Contributor

dcvz commented Mar 13, 2024

Sorry for the delay here! I'm hoping to go through this soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] useActiveTrack() doesnt reflect metadata changes
2 participants