Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing subtitle and audio APIs #224

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions ios/PlayerModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions ios/PlayerModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ export class Player extends NativeInstance<PlayerConfig> {
return PlayerModule.isAirPlayAvailable(this.nativeId);
};

/**
* @returns The currently selected audio track or `null`.
*/
getAudioTrack = async (): Promise<AudioTrack | null> => {
return PlayerModule.getAudioTrack(this.nativeId);
};

/**
* @returns An array containing AudioTrack objects for all available audio tracks.
*/
Expand All @@ -389,6 +396,13 @@ export class Player extends NativeInstance<PlayerConfig> {
return PlayerModule.setAudioTrack(this.nativeId, trackIdentifier);
};

/**
* @returns The currently selected subtitle track or `null`.
*/
getSubtitleTrack = async (): Promise<SubtitleTrack | null> => {
return PlayerModule.getSubtitleTrack(this.nativeId);
};

/**
* @returns An array containing SubtitleTrack objects for all available subtitle tracks.
*/
Expand Down