diff --git a/CHANGELOG.md b/CHANGELOG.md index 7315f7e4..26abe762 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Added + +- `Player.getAudioTrack` and `Player.getSubtitleTrack` APIs to get currently selected audio and subtitle tracks + ## [0.10.0] (2023-09-04) ### Added diff --git a/android/src/main/java/com/bitmovin/player/reactnative/PlayerModule.kt b/android/src/main/java/com/bitmovin/player/reactnative/PlayerModule.kt index c3801115..1a577b76 100644 --- a/android/src/main/java/com/bitmovin/player/reactnative/PlayerModule.kt +++ b/android/src/main/java/com/bitmovin/player/reactnative/PlayerModule.kt @@ -291,6 +291,18 @@ class PlayerModule(private val context: ReactApplicationContext) : ReactContextB } } + /** + * Resolve `nativeId`'s currently selected audio track. + * @param nativeId Target player Id. + * @param promise JS promise object. + */ + @ReactMethod + fun getAudioTrack(nativeId: NativeId, promise: Promise) { + uiManager()?.addUIBlock { + promise.resolve(JsonConverter.fromAudioTrack(players[nativeId]?.source?.selectedAudioTrack)) + } + } + /** * Resolve `nativeId`'s player available audio tracks. * @param nativeId Target player Id. @@ -323,6 +335,18 @@ class PlayerModule(private val context: ReactApplicationContext) : ReactContextB } } + /** + * Resolve `nativeId`'s currently selected subtitle track. + * @param nativeId Target player Id. + * @param promise JS promise object. + */ + @ReactMethod + fun getSubtitleTrack(nativeId: NativeId, promise: Promise) { + uiManager()?.addUIBlock { + promise.resolve(JsonConverter.fromSubtitleTrack(players[nativeId]?.source?.selectedSubtitleTrack)) + } + } + /** * Resolve `nativeId`'s player available subtitle tracks. * @param nativeId Target player Id. diff --git a/ios/PlayerModule.m b/ios/PlayerModule.m index d3d3ad02..0041287c 100644 --- a/ios/PlayerModule.m +++ b/ios/PlayerModule.m @@ -51,6 +51,10 @@ @interface RCT_EXTERN_REMAP_MODULE(PlayerModule, PlayerModule, NSObject) isAirPlayAvailable:(NSString *)nativeId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD( + getAudioTrack:(NSString *)nativeId + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD( getAvailableAudioTracks:(NSString *)nativeId resolver:(RCTPromiseResolveBlock)resolve @@ -60,6 +64,10 @@ @interface RCT_EXTERN_REMAP_MODULE(PlayerModule, PlayerModule, NSObject) trackIdentifier:(NSString *)trackIdentifier resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD( + getSubtitleTrack:(NSString *)nativeId + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD( getAvailableSubtitles:(NSString *)nativeId resolver:(RCTPromiseResolveBlock)resolve diff --git a/ios/PlayerModule.swift b/ios/PlayerModule.swift index 03da8a59..e3fec79a 100644 --- a/ios/PlayerModule.swift +++ b/ios/PlayerModule.swift @@ -359,6 +359,23 @@ class PlayerModule: NSObject, RCTBridgeModule { } } + /** + Resolve `nativeId`'s currently selected audio track. + - Parameter nativeId: Target player Id. + - Parameter resolver: JS promise resolver. + - Parameter rejecter: JS promise rejecter. + */ + @objc(getAudioTrack:resolver:rejecter:) + func getAudioTrack( + _ nativeId: NativeId, + resolver resolve: @escaping RCTPromiseResolveBlock, + rejecter reject: @escaping RCTPromiseRejectBlock + ) { + bridge.uiManager.addUIBlock { [weak self] _, _ in + resolve(RCTConvert.audioTrackJson(self?.players[nativeId]?.audio)) + } + } + /** Resolve `nativeId`'s player available audio tracks. - Parameter nativeId: Target player Id. @@ -399,6 +416,23 @@ class PlayerModule: NSObject, RCTBridgeModule { } } + /** + Resolve `nativeId`'s currently selected subtitle track. + - Parameter nativeId: Target player Id. + - Parameter resolver: JS promise resolver. + - Parameter rejecter: JS promise rejecter. + */ + @objc(getSubtitleTrack:resolver:rejecter:) + func getSubtitleTrack( + _ nativeId: NativeId, + resolver resolve: @escaping RCTPromiseResolveBlock, + rejecter reject: @escaping RCTPromiseRejectBlock + ) { + bridge.uiManager.addUIBlock { [weak self] _, _ in + resolve(RCTConvert.subtitleTrackJson(self?.players[nativeId]?.subtitle)) + } + } + /** Resolve `nativeId`'s player available subtitle tracks. - Parameter nativeId: Target player Id. diff --git a/src/player.ts b/src/player.ts index 2ec68f3f..c76f3c5f 100644 --- a/src/player.ts +++ b/src/player.ts @@ -375,6 +375,13 @@ export class Player extends NativeInstance { return PlayerModule.isAirPlayAvailable(this.nativeId); }; + /** + * @returns The currently selected audio track or `null`. + */ + getAudioTrack = async (): Promise => { + return PlayerModule.getAudioTrack(this.nativeId); + }; + /** * @returns An array containing AudioTrack objects for all available audio tracks. */ @@ -389,6 +396,13 @@ export class Player extends NativeInstance { return PlayerModule.setAudioTrack(this.nativeId, trackIdentifier); }; + /** + * @returns The currently selected subtitle track or `null`. + */ + getSubtitleTrack = async (): Promise => { + return PlayerModule.getSubtitleTrack(this.nativeId); + }; + /** * @returns An array containing SubtitleTrack objects for all available subtitle tracks. */