Skip to content

Commit

Permalink
prefix all new metadata event payloads with “metadata”
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Oct 5, 2023
1 parent 2b53b88 commit 301e620
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions docs/docs/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
27 changes: 25 additions & 2 deletions docs/docs/api/objects/metadata.md
Original file line number Diff line number Diff line change
@@ -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 |
| -------- | -------- | --------------------------------------------------- |
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions ios/RNTrackPlayer/RNTrackPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +22,9 @@ export interface AudioCommonMetadataReceivedEvent {
mediaType: string | undefined;
creationDate: string | undefined;
creationYear: string | undefined;
}

export interface AudioMetadata extends AudioCommonMetadata {
raw: RawEntry[];
}

Expand Down
11 changes: 6 additions & 5 deletions src/interfaces/events/EventPayloadByEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
};

Expand All @@ -55,6 +56,6 @@ type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};

export type EventPayloadByEventWithType = {
[K in keyof EventPayloadByEvent]: EventPayloadByEvent[K] extends never
? { type: K }
: Simplify<EventPayloadByEvent[K] & { type: K }>;
? { type: K }
: Simplify<EventPayloadByEvent[K] & { type: K }>;
};

0 comments on commit 301e620

Please sign in to comment.