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 quality level functionality to Android player #323

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.facebook.react.uimanager.UIManagerModule;
import com.jwplayer.pub.api.JWPlayer;
import com.jwplayer.pub.api.PlayerState;
import com.jwplayer.pub.api.media.adaptive.QualityLevel;
import com.jwplayer.pub.api.configuration.PlayerConfig;
import com.jwplayer.pub.api.media.audio.AudioTrack;
import com.jwplayer.pub.api.media.playlists.PlaylistItem;
Expand Down Expand Up @@ -162,6 +163,84 @@ public void execute (NativeViewHierarchyManager nvhm) {
}
}


@ReactMethod
public void getCurrentQuality(final int reactTag, final Promise promise) {
try {
UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
public void execute (NativeViewHierarchyManager nvhm) {
RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);

if (playerView != null && playerView.mPlayerView != null) {
int quality = playerView.mPlayerView.getPlayer().getCurrentQuality();
promise.resolve(quality);
} else {
promise.reject("RNJW Error", "getCurrentQuality() Player is null");
}
}
});
} catch (IllegalViewOperationException e) {
throw e;
}
}

@ReactMethod
public void setCurrentQuality(final int reactTag, final int index) {
try {
UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
public void execute (NativeViewHierarchyManager nvhm) {
RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);

if (playerView != null && playerView.mPlayerView != null) {
playerView.mPlayerView.getPlayer().setCurrentQuality(index);
}
}
});
} catch (IllegalViewOperationException e) {
throw e;
}
}


@ReactMethod
public void getQualityLevels(final int reactTag, final Promise promise) {
try {
UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
public void execute (NativeViewHierarchyManager nvhm) {
RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);

if (playerView != null && playerView.mPlayerView != null) {
List<QualityLevel> qualityLevelsList = playerView.mPlayerView.getPlayer().getQualityLevels();
if (qualityLevelsList == null) { //if qualitylevels are null than pass empty array.
promise.resolve(null);
return;
}
WritableArray qualityLevels = Arguments.createArray();
for (int i = 0; i < qualityLevelsList.size(); i++) {
WritableMap qualityLevel = Arguments.createMap();
QualityLevel level = qualityLevelsList.get(i);
qualityLevel.putInt("playListPosition", level.getPlaylistPosition());
qualityLevel.putInt("bitRate", level.getBitrate());
qualityLevel.putString("label", level.getLabel());
qualityLevel.putInt("height", level.getHeight());
qualityLevel.putInt("width", level.getWidth());
qualityLevel.putInt("index", level.getTrackIndex());
qualityLevels.pushMap(qualityLevel);
}
promise.resolve(qualityLevels);
} else {
promise.reject("RNJW Error", "getQualityLevels() Player is null");
}
}
});
} catch (IllegalViewOperationException e) {
throw e;
}
}

@ReactMethod
public void pause(final int reactTag) {
try {
Expand Down
13 changes: 13 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ declare module "react-native-jw-media-player" {
language: string;
name: string;
}

export interface QualityLevel {
playListPosition: number;
bitRate: number;
label: string;
height: number;
width: number;
index: number;
}

interface CastingDevice {
name?: string;
identifier?: string;
Expand Down Expand Up @@ -203,6 +213,9 @@ declare module "react-native-jw-media-player" {
stop(): void;
toggleSpeed(): void;
setSpeed(speed: number): void;
setCurrentQuality(index: number): void;
currentQuality(): number;
getQualityLevels(): Promise<QualityLevel[] | null>;
setVolume(volume: number): void;
setPlaylistIndex(index: number): void;
setControls(show: boolean): void;
Expand Down
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ export default class JWPlayer extends Component {
setVolume: PropTypes.func,
toggleSpeed: PropTypes.func,
setSpeed: PropTypes.func,
setCurrentQuality: PropTypes.func,
currentQuality: PropTypes.func,
getQualityLevels: PropTypes.func,
setPlaylistIndex: PropTypes.func,
setControls: PropTypes.func,
setVisibility: PropTypes.func,
Expand Down Expand Up @@ -319,6 +322,35 @@ export default class JWPlayer extends Component {
RNJWPlayerManager.setSpeed(this.getRNJWPlayerBridgeHandle(), speed);
}

setCurrentQuality(index) {
if (RNJWPlayerManager && Platform.OS === "android")
RNJWPlayerManager.setCurrentQuality(
this.getRNJWPlayerBridgeHandle(),
index
);
}

currentQuality() {
if (RNJWPlayerManager && Platform.OS === "android")
return RNJWPlayerManager.getCurrentQuality(
this.getRNJWPlayerBridgeHandle()
);
}

async getQualityLevels() {
if (RNJWPlayerManager && Platform.OS === "android") {
try {
var qualityLevels = await RNJWPlayerManager.getQualityLevels(
this.getRNJWPlayerBridgeHandle()
);
return qualityLevels;
} catch (e) {
console.error(e);
return null;
}
}
}

setPlaylistIndex(index) {
if (RNJWPlayerManager)
RNJWPlayerManager.setPlaylistIndex(
Expand Down