react-native-track-player icon indicating copy to clipboard operation
react-native-track-player copied to clipboard

updateMetadataForTrack not working most of the time

Open RamProg opened this issue 3 years ago • 3 comments
trafficstars

Describe the Bug updateMetadataForTrack is not changing the notification and the Now Playing Center on Android. I'm leaving an example with the title property changing, but the same happens with the duration and with every other property.

Steps To Reproduce On Android Add a track to the queue, play it and try to update the notification with the updateMetadataForTrack. You should see a change in the notification, but that's not happening. If you get the track you'll see the changes, but the notification always stays the same (I think it worked once or twice, but fails most times).

Code To Reproduce

const oldTitle = (await TrackPlayer.getTrack(0))?.title;
console.log('old Title: ', oldTitle);
await TrackPlayer.updateMetadataForTrack(0, {
    title: 'has changed',
});
const newTitle = (await TrackPlayer.getTrack(0))?.title;
console.log('newTitle', newTitle);
// oldTitle: 'My favourite Song'
// newTitle: 'hasChanged'

Environment Info:

System:
    OS: macOS 12.2.1
    CPU: (10) x64 Apple M1 Max
    Memory: 28.82 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.15.0 - ~/.nvm/versions/node/v14.15.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v14.15.0/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v14.15.0/bin/npm
    Watchman: 2022.07.04.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK: Not Found
  IDEs:
    Android Studio: 2021.2 AI-212.5712.43.2112.8609683
    Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.16 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: Not Found
    react-native: Not Found
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

react-native-track-player v3.1.0 Also happens in nightly. Android Studio Simulator and device. MacOS.

How I can Help I can provide support and data.

RamProg avatar Sep 09 '22 11:09 RamProg

Having the same issue but my Artwork is updating correctly just not title or artist. iOS works fine.

I tried

TrackPlayer.updateNowPlayingMetadata({ title: String(title), artist: String(artist), artwork: img === '' ? metadata.artwork : img, });

This is the same code I used in v2.2.0-rc3 and updated fine with that version.

Tucker2015 avatar Sep 10 '22 13:09 Tucker2015

Yeah, my issue is with the duration because I'm trying to get the duration and then update it, but I noticed it also fails with other properties. I haven't tried the updateNowPlayingMetadata, but if it's not working for you it's probably the same issue.

RamProg avatar Sep 12 '22 09:09 RamProg

I have the same issue in android, using the same code as before, and I have null for the artist and title, and sometimes the artwork works

VinceBT avatar Sep 13 '22 22:09 VinceBT

I've been attempting to debug this. I've traced the metadata through to KotlinAudio's NotificationManager class.

The notificationMetadata property is set with the artist, title and artworkUrl. If you insert a log here, you can see the values as expected.

That then calls the reload method, which in turn calls the invalidate method on Exoplayer's notification manager.

This is where the trail currently runs cold for me.

If you comment out the call to reload(), you will see the notification no longer updates at all. Leave the call to reload() in, and only the artwork updates, but not the artist or title.

For anyone looking to replicate, you need to pull this repo and KotlinAudio and set everything up locally as explained in the example project's README.

Then edit PlayerControls.tsx in the example project, to include a way to update metadata, like this:

export const PlayerControls: React.FC = () => {

  const sendMeta = async () => {
    const trackIndex = await TrackPlayer.getCurrentTrack();
    if (trackIndex !== null) {
      const metadata = {
        artist: 'HELLO',
        title: 'WORLD',
        artwork: 'https://is2-ssl.mzstatic.com/image/thumb/Music122/v4/ca/0e/ec/ca0eeccb-eecd-4c72-557d-3d422c6dd1c6/5054197264252.jpg/600x600bb.jpg',
      };

      TrackPlayer.updateMetadataForTrack(trackIndex, metadata);
    }
  };

  return (
    <View style={{ width: '100%' }}>
      <View style={styles.row}>
        <Button
          title="Prev"
          onPress={() => TrackPlayer.skipToPrevious()}
          type="secondary"
        />
        <PlayPauseButton />
        <Button
          title="Next"
          onPress={() => TrackPlayer.skipToNext()}
          type="secondary"
        />
        <Button
            title="Meta"
            onPress={sendMeta}
            type="secondary"
        />
      </View>
    </View>
  );
};

You will note that the notification updates correctly with each track that is played, but when you tap the "Meta" button, only the artwork changes - the artist and title remain as they were.

gavrichards avatar Sep 23 '22 10:09 gavrichards

@mpivchev could use your eyes on this one. I took a high-level look. Not sure why the ExoPlayer invalidate method isn't doing what its supposed to be (at least not entirely).

jspizziri avatar Sep 23 '22 10:09 jspizziri

I've stumbled on a fix, but I don't fully understand how/why it works. It involves this section here.

If you change:

    fun replaceItem(index: Int, item: AudioItem) {
        val mediaSource = getMediaSourceFromAudioItem(item)
        queue[index] = mediaSource

        if (currentIndex == index && automaticallyUpdateNotificationMetadata)
            notificationManager.notificationMetadata = NotificationMetadata(item.title, item.artist, item.artwork)
    }

to:

    fun replaceItem(index: Int, item: AudioItem) {
        val mediaSource = getMediaSourceFromAudioItem(item)
        queue[index] = mediaSource

        if (currentIndex == index && automaticallyUpdateNotificationMetadata) {
            notificationManager.notificationMetadata =
                NotificationMetadata(item.title, item.artist, item.artwork)

            mediaSessionConnector.setMediaMetadataProvider {
                mediaSource.getMediaMetadataCompat()
            }
        }
    }

...then it works.

I discovered this because I noticed how the call to set notificationManager.notificationMetadata in BaseAudioPlayer is followed by a similar snippet (but I was able to simplify it for this fix).

I would definitely appreciate @mpivchev reviewing this. Particularly how automaticallyUpdateNotificationMetadata is used in both sections I've referenced.

gavrichards avatar Sep 23 '22 17:09 gavrichards

Possibly related to https://github.com/doublesymmetry/react-native-track-player/issues/1653?

@gavrichards if its convenient I'd be curious to see if your fix also fixes the issue above.

jspizziri avatar Sep 26 '22 13:09 jspizziri

@jspizziri I'll give it a quick try now.

gavrichards avatar Sep 26 '22 13:09 gavrichards

I updated to RNTP v3.2 and this issue is still happening.

RamProg avatar Oct 07 '22 11:10 RamProg

I updated to RNTP v3.2 and this issue is still happening.

The PR https://github.com/doublesymmetry/KotlinAudio/pull/52 hasn't been reviewed yet, so the bug is still present.

gavrichards avatar Oct 07 '22 11:10 gavrichards

hi, I'm having an issue where the updateMetadataForTrack is not working. I use it when I recieve the song details from a radio live (using icy or icy-headers) inside the "PlaybackMetadataReceived" event

I'm currently on "react-native-track-player": "^3.2.0",

neoassyrian avatar Oct 21 '22 05:10 neoassyrian

I was also able to fix this by removing the original mediaSessionConnector.setMediaMetadataProvider call. See https://github.com/doublesymmetry/KotlinAudio/pull/54/commits/9db93a959387485cadfbe29d2cce199b52e1eb5f

It seems that it was causing exoplayer to update the notification as well. @mpivchev I see you introduced this in the following pr https://github.com/doublesymmetry/KotlinAudio/commit/d2ac459a90498aa4487358e5716979aba558bffe – perhaps there is a good reason for adding it that I may be missing?

puckey avatar Nov 04 '22 11:11 puckey

Still happening on 3.2.0-d23c3fb990aaf9d6783881fabe21e059b62b0782

RamProg avatar Nov 05 '22 14:11 RamProg

Hi! I'm also seeing the issue on Android devices & 3.2.0. Any expected version for the review/fix? Thanks!

gianpietro1 avatar Jan 13 '23 23:01 gianpietro1

In the current nightly, artist and title is now working, but the artwork no longer updates, getting stuck on the initial image for the track.

gavrichards avatar Feb 02 '23 22:02 gavrichards

Not sure if this helps, but I'm not seeing the issue at all in API 28 (ZTE Blade A530 with Android 9)

gianpietro1 avatar Feb 03 '23 17:02 gianpietro1

Hi, If it can helps, I only view this on android 11 (api 30). On API 33 it seems to work (but I only try on emulator)

DrMickArtisan avatar Feb 06 '23 09:02 DrMickArtisan

In the current nightly, artist and title is now working, but the artwork no longer updates, getting stuck on the initial image for the track.

Should be fixed by https://github.com/doublesymmetry/KotlinAudio/pull/59

puckey avatar Feb 06 '23 14:02 puckey

Now that https://github.com/doublesymmetry/KotlinAudio/pull/59 has been merged we'll need to wait for the build to be available on jitpack.io and then bump the version here https://github.com/doublesymmetry/react-native-track-player/blob/main/android/build.gradle#L52

Any community members interested in creating a PR for that? Should be a chipshot.

jspizziri avatar Feb 06 '23 15:02 jspizziri

This is now fixed in nightly, issue can be closed.

gavrichards avatar Feb 08 '23 09:02 gavrichards

I am still running into this issue on Android.

I don't know for sure my problem isn't user error, but I am using the nightly branch, and it seems like KotlinAudio 2.0.0-rc3 is installed (I'm not 100% sure how to check this but I see it in the RNTP build.gradle).

The image on the lockscreen stays whatever the image was that first loaded for the track.

I call await PVAudioPlayer.updateMetadataForTrack(currentIndex, newTrack) and the new track has different artwork URLs (and open the URL in a browser to verify the images are different), yet the lock screen stays the same image.

mitchdowney avatar Mar 06 '23 22:03 mitchdowney

This issue was fixed in 4.0.0-rc05 but seems to have come back in 4.0.0-rc06.

tewson avatar Aug 01 '23 19:08 tewson

I can confirm we're seeing this regression too, after updating from 4.0.0-rc04 to 4.0.0-rc06.

gavrichards avatar Aug 02 '23 08:08 gavrichards

So I think we also need to update the tag of the exoplayer media item here: https://github.com/doublesymmetry/KotlinAudio/blob/main/kotlin-audio/src/main/java/com/doublesymmetry/kotlinaudio/players/QueuedAudioPlayer.kt#L217

Will be able to look into it early September

puckey avatar Aug 10 '23 13:08 puckey

There's a fix for this ongoing here: https://github.com/doublesymmetry/KotlinAudio/pull/87. I'll update this thread when that's done.

dcvz avatar Aug 10 '23 13:08 dcvz

This should be fixed now in v4.0.0.0-rc07

dcvz avatar Aug 10 '23 23:08 dcvz