From 301e6200d540acc42715b0e878439a5af2f4bb97 Mon Sep 17 00:00:00 2001 From: dcvz Date: Thu, 5 Oct 2023 16:51:36 +0200 Subject: [PATCH 1/2] =?UTF-8?q?prefix=20all=20new=20metadata=20event=20pay?= =?UTF-8?q?loads=20with=20=E2=80=9Cmetadata=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../trackplayer/module/MusicEvents.kt | 1 + .../trackplayer/service/MusicService.kt | 11 ++++++-- docs/docs/api/events.md | 6 ++--- docs/docs/api/objects/metadata.md | 27 +++++++++++++++++-- ios/RNTrackPlayer/RNTrackPlayer.swift | 6 ++--- ...Event.ts => AudioMetadataReceivedEvent.ts} | 11 ++++++++ src/interfaces/events/EventPayloadByEvent.ts | 11 ++++---- 7 files changed, 58 insertions(+), 15 deletions(-) rename src/interfaces/events/{AudioCommonMetadataReceivedEvent.ts => AudioMetadataReceivedEvent.ts} (77%) diff --git a/android/src/main/java/com/doublesymmetry/trackplayer/module/MusicEvents.kt b/android/src/main/java/com/doublesymmetry/trackplayer/module/MusicEvents.kt index 697e81a8b..9a2ee9017 100644 --- a/android/src/main/java/com/doublesymmetry/trackplayer/module/MusicEvents.kt +++ b/android/src/main/java/com/doublesymmetry/trackplayer/module/MusicEvents.kt @@ -50,6 +50,7 @@ class MusicEvents(private val reactContext: ReactContext) : BroadcastReceiver() const val METADATA_CHAPTER_RECEIVED = "metadata-chapter-received" const val METADATA_TIMED_RECEIVED = "metadata-timed-received" const val METADATA_COMMON_RECEIVED = "metadata-common-received" + const val METADATA_PAYLOAD_KEY = "metadata" // Other const val PLAYER_ERROR = "player-error" diff --git a/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt b/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt index 842d16e5e..88be0e21e 100644 --- a/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt +++ b/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt @@ -26,6 +26,7 @@ import com.doublesymmetry.trackplayer.model.PlaybackMetadata import com.doublesymmetry.trackplayer.model.Track import com.doublesymmetry.trackplayer.model.TrackAudioItem import com.doublesymmetry.trackplayer.module.MusicEvents +import com.doublesymmetry.trackplayer.module.MusicEvents.Companion.METADATA_PAYLOAD_KEY import com.doublesymmetry.trackplayer.utils.BundleUtils import com.doublesymmetry.trackplayer.utils.BundleUtils.setRating import com.facebook.react.HeadlessJsTaskService @@ -662,7 +663,10 @@ class MusicService : HeadlessJsTaskService() { scope.launch { event.onTimedMetadata.collect { val data = MetadataAdapter.fromMetadata(it) - emitList(MusicEvents.METADATA_TIMED_RECEIVED, data) + val bundle = Bundle().apply { + putParcelableArrayList(METADATA_PAYLOAD_KEY, ArrayList(data)) + } + emit(MusicEvents.METADATA_TIMED_RECEIVED, bundle) // TODO: Handle the different types of metadata and publish to new events val metadata = PlaybackMetadata.fromId3Metadata(it) @@ -688,7 +692,10 @@ class MusicService : HeadlessJsTaskService() { scope.launch { event.onCommonMetadata.collect { val data = MetadataAdapter.fromMediaMetadata(it) - emit(MusicEvents.METADATA_COMMON_RECEIVED, data) + val bundle = Bundle().apply { + putBundle(METADATA_PAYLOAD_KEY, data) + } + emit(MusicEvents.METADATA_COMMON_RECEIVED, bundle) } } diff --git a/docs/docs/api/events.md b/docs/docs/api/events.md index a1ea89558..dc04069d5 100644 --- a/docs/docs/api/events.md +++ b/docs/docs/api/events.md @@ -209,14 +209,14 @@ event in the following situations: ### `AudioCommonMetadataReceived` Fired when the current track receives metadata encoded in - static metadata not tied to a time. Usually received at start. -Received data will be [`AudioCommonMetadataReceivedEvent`](./api/objects/metadata.md) - `raw` will always be `undefined`. +Received data will be [`AudioCommonMetadataReceivedEvent`](./api/objects/metadata.md). ### `AudioTimedMetadataReceived` Fired when the current track receives metadata encoded in - dynamic metadata tied to a time. Events may be emitted over time. -Received data will be [`AudioTimedMetadataReceivedEvent`](./api/objects/metadata.md). +Received data will be [`AudioMetadataReceivedEvent`](./api/objects/metadata.md). ### `AudioChapterMetadataReceived` (iOS only) Fired when the current track receives metadata encoded in - chapter overview data. Usually received at start. -Received data will be [`AudioChapterMetadataReceivedEvent`](./api/objects/metadata.md). +Received data will be [`AudioMetadataReceivedEvent`](./api/objects/metadata.md). diff --git a/docs/docs/api/objects/metadata.md b/docs/docs/api/objects/metadata.md index bb7ef23de..e69945da5 100644 --- a/docs/docs/api/objects/metadata.md +++ b/docs/docs/api/objects/metadata.md @@ -1,6 +1,22 @@ +# AudioMetadataReceivedEvent + +An object representing the timed or chapter metadata received for a track. + +| Param | Type | Description | +| -------- | -------- | --------------------------------------------------- | +| metadata | `AudioMetadata[]` | The metadata received | + # AudioCommonMetadataReceivedEvent -An object representing the metadata received for a track. +An object representing the common metadata received for a track. + +| Param | Type | Description | +| -------- | -------- | --------------------------------------------------- | +| metadata | `AudioCommonMetadata` | The metadata received | + +# AudioCommonMetadata + +An object representing the common metadata received for a track. | Param | Type | Description | | -------- | -------- | --------------------------------------------------- | @@ -19,7 +35,14 @@ An object representing the metadata received for a track. | mediaType | `string` | The track media type. Might be undefined | | creationDate | `string` | The track creation date. Might be undefined | | creationYear | `string` | The track creation year. Might be undefined | -| raw | `RawEntry[]` | The raw metadata that was used to populate. May contain other non common keys. Might be undefined | + +# AudioMetadata + +An extension of `AudioCommonMetadataReceivedEvent` that includes the raw metadata. + +| Param | Type | Description | +| -------- | -------- | --------------------------------------------------- | +| raw | `RawEntry[]` | The raw metadata that was used to populate. May contain other non common keys. May be empty | # RawEntry diff --git a/ios/RNTrackPlayer/RNTrackPlayer.swift b/ios/RNTrackPlayer/RNTrackPlayer.swift index 002892c98..d0fac0e0d 100644 --- a/ios/RNTrackPlayer/RNTrackPlayer.swift +++ b/ios/RNTrackPlayer/RNTrackPlayer.swift @@ -814,17 +814,17 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { func handleAudioPlayerCommonMetadataReceived(metadata: [AVMetadataItem]) { let commonMetadata = MetadataAdapter.convertToCommonMetadata(metadata: metadata, skipRaw: true) - emit(event: EventType.MetadataCommonReceived, body: commonMetadata) + emit(event: EventType.MetadataCommonReceived, body: ["metadata": commonMetadata]) } func handleAudioPlayerChapterMetadataReceived(metadata: [AVTimedMetadataGroup]) { let metadataItems = MetadataAdapter.convertToGroupedMetadata(metadataGroups: metadata); - emit(event: EventType.MetadataChapterReceived, body: metadataItems) + emit(event: EventType.MetadataChapterReceived, body: ["metadata": metadataItems]) } func handleAudioPlayerTimedMetadataReceived(metadata: [AVTimedMetadataGroup]) { let metadataItems = MetadataAdapter.convertToGroupedMetadata(metadataGroups: metadata); - emit(event: EventType.MetadataTimedReceived, body: metadataItems) + emit(event: EventType.MetadataTimedReceived, body: ["metadata": metadataItems]) // SwiftAudioEx was updated to return the array of timed metadata // Until we have support for that in RNTP, we take the first item to keep existing behaviour. diff --git a/src/interfaces/events/AudioCommonMetadataReceivedEvent.ts b/src/interfaces/events/AudioMetadataReceivedEvent.ts similarity index 77% rename from src/interfaces/events/AudioCommonMetadataReceivedEvent.ts rename to src/interfaces/events/AudioMetadataReceivedEvent.ts index 75c800d90..f42723478 100644 --- a/src/interfaces/events/AudioCommonMetadataReceivedEvent.ts +++ b/src/interfaces/events/AudioMetadataReceivedEvent.ts @@ -1,4 +1,12 @@ +export interface AudioMetadataReceivedEvent { + metadata: AudioMetadata[]; +} + export interface AudioCommonMetadataReceivedEvent { + metadata: AudioCommonMetadata; +} + +export interface AudioCommonMetadata { title: string | undefined; artist: string | undefined; albumTitle: string | undefined; @@ -14,6 +22,9 @@ export interface AudioCommonMetadataReceivedEvent { mediaType: string | undefined; creationDate: string | undefined; creationYear: string | undefined; +} + +export interface AudioMetadata extends AudioCommonMetadata { raw: RawEntry[]; } diff --git a/src/interfaces/events/EventPayloadByEvent.ts b/src/interfaces/events/EventPayloadByEvent.ts index 0e90c557f..6dca90f89 100644 --- a/src/interfaces/events/EventPayloadByEvent.ts +++ b/src/interfaces/events/EventPayloadByEvent.ts @@ -4,7 +4,8 @@ import type { PlaybackState } from '../PlaybackState'; import type { PlaybackActiveTrackChangedEvent } from './PlaybackActiveTrackChangedEvent'; import type { PlaybackErrorEvent } from './PlaybackErrorEvent'; import type { PlaybackMetadataReceivedEvent } from './PlaybackMetadataReceivedEvent'; -import type { AudioCommonMetadataReceivedEvent } from './AudioCommonMetadataReceivedEvent'; +import type { AudioMetadataReceivedEvent } from './AudioMetadataReceivedEvent'; +import type { AudioCommonMetadataReceivedEvent } from './AudioMetadataReceivedEvent'; import type { PlaybackPlayWhenReadyChangedEvent } from './PlaybackPlayWhenReadyChangedEvent'; import type { PlaybackProgressUpdatedEvent } from './PlaybackProgressUpdatedEvent'; import type { PlaybackQueueEndedEvent } from './PlaybackQueueEndedEvent'; @@ -45,8 +46,8 @@ export type EventPayloadByEvent = { [Event.RemoteLike]: never; [Event.RemoteDislike]: never; [Event.RemoteBookmark]: never; - [Event.MetadataChapterReceived]: AudioCommonMetadataReceivedEvent[]; - [Event.MetadataTimedReceived]: AudioCommonMetadataReceivedEvent[]; + [Event.MetadataChapterReceived]: AudioMetadataReceivedEvent; + [Event.MetadataTimedReceived]: AudioMetadataReceivedEvent; [Event.MetadataCommonReceived]: AudioCommonMetadataReceivedEvent; }; @@ -55,6 +56,6 @@ type Simplify = { [KeyType in keyof T]: T[KeyType] } & {}; export type EventPayloadByEventWithType = { [K in keyof EventPayloadByEvent]: EventPayloadByEvent[K] extends never - ? { type: K } - : Simplify; + ? { type: K } + : Simplify; }; From c2f2e194efcbc8fa1d042a80f68de579c81e862d Mon Sep 17 00:00:00 2001 From: dcvz Date: Thu, 5 Oct 2023 16:56:47 +0200 Subject: [PATCH 2/2] Run formatter --- src/interfaces/events/EventPayloadByEvent.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interfaces/events/EventPayloadByEvent.ts b/src/interfaces/events/EventPayloadByEvent.ts index 6dca90f89..a9d11920a 100644 --- a/src/interfaces/events/EventPayloadByEvent.ts +++ b/src/interfaces/events/EventPayloadByEvent.ts @@ -56,6 +56,6 @@ type Simplify = { [KeyType in keyof T]: T[KeyType] } & {}; export type EventPayloadByEventWithType = { [K in keyof EventPayloadByEvent]: EventPayloadByEvent[K] extends never - ? { type: K } - : Simplify; + ? { type: K } + : Simplify; };