Commit 9d4cbd4e by samrobinson Committed by Oliver Woodman

Update MediaMetadata from static and dynamic metadata.

PiperOrigin-RevId: 369671127
parent e0502cb3
...@@ -18,6 +18,7 @@ package com.google.android.exoplayer2; ...@@ -18,6 +18,7 @@ package com.google.android.exoplayer2;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
...@@ -29,20 +30,37 @@ public final class MediaMetadata implements Bundleable { ...@@ -29,20 +30,37 @@ public final class MediaMetadata implements Bundleable {
/** A builder for {@link MediaMetadata} instances. */ /** A builder for {@link MediaMetadata} instances. */
public static final class Builder { public static final class Builder {
@Nullable private String title;
public Builder() {} public Builder() {}
private Builder(MediaMetadata mediaMetadata) { private Builder(MediaMetadata mediaMetadata) {
this.title = mediaMetadata.title; this.title = mediaMetadata.title;
} }
@Nullable private String title;
/** Sets the optional title. */ /** Sets the optional title. */
public Builder setTitle(@Nullable String title) { public Builder setTitle(@Nullable String title) {
this.title = title; this.title = title;
return this; return this;
} }
/**
* Sets all fields supported by the {@link Metadata.Entry entries} within the {@link Metadata}.
*
* <p>Fields are only set if the {@link Metadata.Entry} has an implementation for {@link
* Metadata.Entry#populateMediaMetadata(Builder)}.
*
* <p>In the event that multiple {@link Metadata.Entry} objects within the {@link Metadata}
* relate to the same {@link MediaMetadata} field, then the last one will be used.
*/
public Builder populateFromMetadata(Metadata metadata) {
for (int i = 0; i < metadata.length(); i++) {
Metadata.Entry entry = metadata.get(i);
entry.populateMediaMetadata(this);
}
return this;
}
/** Returns a new {@link MediaMetadata} instance with the current builder values. */ /** Returns a new {@link MediaMetadata} instance with the current builder values. */
public MediaMetadata build() { public MediaMetadata build() {
return new MediaMetadata(/* builder= */ this); return new MediaMetadata(/* builder= */ this);
......
...@@ -151,6 +151,17 @@ public interface Player { ...@@ -151,6 +151,17 @@ public interface Player {
default void onStaticMetadataChanged(List<Metadata> metadataList) {} default void onStaticMetadataChanged(List<Metadata> metadataList) {}
/** /**
* Called when the combined {@link MediaMetadata} changes.
*
* <p>The provided {@link MediaMetadata} is a combination of the {@link MediaItem#mediaMetadata}
* and the static and dynamic metadata sourced from {@link
* EventListener#onStaticMetadataChanged(List)} and {@link MetadataOutput#onMetadata(Metadata)}.
*
* @param mediaMetadata The combined {@link MediaMetadata}.
*/
default void onMediaMetadataChanged(MediaMetadata mediaMetadata) {}
/**
* Called when the player starts or stops loading the source. * Called when the player starts or stops loading the source.
* *
* <p>{@link #onEvents(Player, Events)} will also be called to report this event along with * <p>{@link #onEvents(Player, Events)} will also be called to report this event along with
......
...@@ -51,6 +51,10 @@ public final class Metadata implements Parcelable { ...@@ -51,6 +51,10 @@ public final class Metadata implements Parcelable {
/** /**
* Updates the {@link MediaMetadata.Builder} with the type specific values stored in this Entry. * Updates the {@link MediaMetadata.Builder} with the type specific values stored in this Entry.
* *
* <p>The order of the {@link Entry} objects in the {@link Metadata} matters. If two {@link
* Entry} entries attempt to populate the same {@link MediaMetadata} field, then the last one in
* the list is used.
*
* @param builder The builder to be updated. * @param builder The builder to be updated.
*/ */
default void populateMediaMetadata(MediaMetadata.Builder builder) {} default void populateMediaMetadata(MediaMetadata.Builder builder) {}
......
...@@ -2067,6 +2067,14 @@ public class SimpleExoPlayer extends BasePlayer ...@@ -2067,6 +2067,14 @@ public class SimpleExoPlayer extends BasePlayer
return keepSessionIdAudioTrack.getAudioSessionId(); return keepSessionIdAudioTrack.getAudioSessionId();
} }
private void setMediaMetadata(MediaMetadata mediaMetadata) {
if (this.currentMediaMetadata.equals(mediaMetadata)) {
return;
}
this.currentMediaMetadata = mediaMetadata;
componentListener.onMediaMetadataChanged(this.currentMediaMetadata);
}
private static DeviceInfo createDeviceInfo(StreamVolumeManager streamVolumeManager) { private static DeviceInfo createDeviceInfo(StreamVolumeManager streamVolumeManager) {
return new DeviceInfo( return new DeviceInfo(
DeviceInfo.PLAYBACK_TYPE_LOCAL, DeviceInfo.PLAYBACK_TYPE_LOCAL,
...@@ -2242,6 +2250,7 @@ public class SimpleExoPlayer extends BasePlayer ...@@ -2242,6 +2250,7 @@ public class SimpleExoPlayer extends BasePlayer
@Override @Override
public void onMetadata(Metadata metadata) { public void onMetadata(Metadata metadata) {
analyticsCollector.onMetadata(metadata); analyticsCollector.onMetadata(metadata);
setMediaMetadata(getMediaMetadata().buildUpon().populateFromMetadata(metadata).build());
for (MetadataOutput metadataOutput : metadataOutputs) { for (MetadataOutput metadataOutput : metadataOutputs) {
metadataOutput.onMetadata(metadata); metadataOutput.onMetadata(metadata);
} }
...@@ -2372,6 +2381,16 @@ public class SimpleExoPlayer extends BasePlayer ...@@ -2372,6 +2381,16 @@ public class SimpleExoPlayer extends BasePlayer
} }
@Override @Override
public void onStaticMetadataChanged(List<Metadata> metadataList) {
MediaMetadata.Builder metadataBuilder = getMediaMetadata().buildUpon();
for (int i = 0; i < metadataList.size(); i++) {
metadataBuilder.populateFromMetadata(metadataList.get(i));
}
setMediaMetadata(metadataBuilder.build());
}
@Override
public void onPlaybackStateChanged(@State int playbackState) { public void onPlaybackStateChanged(@State int playbackState) {
updateWakeAndWifiLock(); updateWakeAndWifiLock();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment