Skip to content

Commit

Permalink
chore(sample): fix default track identification and add audio tracks …
Browse files Browse the repository at this point in the history
…selection option (#4184)
  • Loading branch information
freeboub authored Sep 20, 2024
1 parent 6fedca0 commit c96f7d4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
8 changes: 4 additions & 4 deletions examples/basic/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ PODS:
- React-Mapbuffer (0.74.5):
- glog
- React-debug
- react-native-video (6.5.0):
- react-native-video (6.6.1):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1008,7 +1008,7 @@ PODS:
- React-featureflags
- React-graphics
- React-ImageManager
- react-native-video/Video (= 6.5.0)
- react-native-video/Video (= 6.6.1)
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
Expand Down Expand Up @@ -1038,7 +1038,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-video/Video (6.5.0):
- react-native-video/Video (6.6.1):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1559,7 +1559,7 @@ SPEC CHECKSUMS:
React-jsitracing: c83efb63c8e9e1dff72a3c56e88ae1c530a87795
React-logger: 257858bd55f3a4e1bc0cf07ddc8fb9faba6f8c7c
React-Mapbuffer: dce508662b995ffefd29e278a16b78217039d43d
react-native-video: 9f0b3c888537f9ded698f762f541b13573dd3686
react-native-video: 8c6d22c8eedf4359fca199e217e4f53581f7b4ab
react-native-video-plugin-sample: d3a93b7ad777cad7fa2c30473de75a2635ce5feb
React-nativeconfig: f326487bc61eba3f0e328da6efb2711533dcac46
React-NativeModulesApple: d89733f5baed8b9249ca5a8e497d63c550097312
Expand Down
52 changes: 27 additions & 25 deletions examples/basic/src/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
isAndroid,
srcList,
textTracksSelectionBy,
audioTracksSelectionBy,
} from './constants';
import {Overlay, toast, VideoLoader} from './components';
import * as NavigationBar from 'expo-navigation-bar';
Expand Down Expand Up @@ -117,18 +118,23 @@ const VideoPlayer: FC<Props> = ({}) => {
}, []);

const onAudioTracks = (data: OnAudioTracksData) => {
console.log('onAudioTracks', data);
const selectedTrack = data.audioTracks?.find((x: AudioTrack) => {
return x.selected;
});
if (selectedTrack?.index) {
setAudioTracks(data.audioTracks);
setSelectedAudioTrack({
type: SelectedTrackType.INDEX,
value: selectedTrack.index,
});
} else {
setAudioTracks(data.audioTracks);
let value;
if (audioTracksSelectionBy === SelectedTrackType.INDEX) {
value = selectedTrack?.index;
} else if (audioTracksSelectionBy === SelectedTrackType.LANGUAGE) {
value = selectedTrack?.language;
} else if (audioTracksSelectionBy === SelectedTrackType.TITLE) {
value = selectedTrack?.title;
}
setAudioTracks(data.audioTracks);
setSelectedAudioTrack({
type: audioTracksSelectionBy,
value: value,
});
};

const onVideoTracks = (data: OnVideoTracksData) => {
Expand All @@ -141,23 +147,19 @@ const VideoPlayer: FC<Props> = ({}) => {
return x?.selected;
});

if (selectedTrack?.language) {
setTextTracks(data.textTracks);
let value;
if (textTracksSelectionBy === SelectedTrackType.INDEX) {
value = selectedTrack?.index;
} else if (textTracksSelectionBy === SelectedTrackType.LANGUAGE) {
value = selectedTrack?.language;
} else if (textTracksSelectionBy === SelectedTrackType.TITLE) {
value = selectedTrack?.title;
}
setSelectedTextTrack({
type: textTracksSelectionBy,
value: value,
});
} else {
setTextTracks(data.textTracks);
setTextTracks(data.textTracks);
let value;
if (textTracksSelectionBy === SelectedTrackType.INDEX) {
value = selectedTrack?.index;
} else if (textTracksSelectionBy === SelectedTrackType.LANGUAGE) {
value = selectedTrack?.language;
} else if (textTracksSelectionBy === SelectedTrackType.TITLE) {
value = selectedTrack?.title;
}
setSelectedTextTrack({
type: textTracksSelectionBy,
value: value,
});
};

const onLoad = (data: OnLoadData) => {
Expand Down Expand Up @@ -246,7 +248,7 @@ const VideoPlayer: FC<Props> = ({}) => {
const _bufferConfig = {
...bufferConfig,
cacheSizeMB: useCache ? 200 : 0,
}
};

return (
<View style={styles.container}>
Expand Down
26 changes: 20 additions & 6 deletions examples/basic/src/components/AudioTracksSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import {Picker} from '@react-native-picker/picker';
import {Text} from 'react-native';
import type {AudioTrack, SelectedTrack} from 'react-native-video';
import {
SelectedTrackType,
type AudioTrack,
type SelectedTrack,
} from 'react-native-video';
import styles from '../styles';
import React from 'react';

export interface AudioTrackSelectorType {
audioTracks: Array<AudioTrack>;
selectedAudioTrack: SelectedTrack | undefined;
onValueChange: (arg0: string) => void;
onValueChange: (arg0: string | number) => void;
audioTracksSelectionBy: SelectedTrackType;
}

export const AudioTrackSelector = ({
audioTracks,
selectedAudioTrack,
onValueChange,
audioTracksSelectionBy,
}: AudioTrackSelectorType) => {
return (
<>
Expand All @@ -25,7 +31,7 @@ export const AudioTrackSelector = ({
onValueChange={itemValue => {
if (itemValue !== 'empty') {
console.log('on audio value change ' + itemValue);
onValueChange(`${itemValue}`);
onValueChange(itemValue);
}
}}>
{audioTracks?.length <= 0 ? (
Expand All @@ -37,11 +43,19 @@ export const AudioTrackSelector = ({
if (!track) {
return;
}
let value;
if (audioTracksSelectionBy === SelectedTrackType.INDEX) {
value = track.index;
} else if (audioTracksSelectionBy === SelectedTrackType.LANGUAGE) {
value = track.language;
} else if (audioTracksSelectionBy === SelectedTrackType.TITLE) {
value = track.title;
}
return (
<Picker.Item
label={`${track.language} - ${track.title} - ${track.selected}`}
value={`${track.index}`}
key={`${track.index}`}
label={`${value} - ${track.selected}`}
value={`${value}`}
key={`${value}`}
/>
);
})}
Expand Down
15 changes: 9 additions & 6 deletions examples/basic/src/components/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import React, {
} from 'react';
import {View} from 'react-native';
import styles from '../styles.tsx';
import {isAndroid, isIos, textTracksSelectionBy} from '../constants';
import {
isAndroid,
isIos,
textTracksSelectionBy,
audioTracksSelectionBy,
} from '../constants';
import {
ResizeMode,
VideoRef,
Expand Down Expand Up @@ -148,17 +153,14 @@ const _Overlay = forwardRef<VideoRef, Props>((props, ref) => {
setShowNotificationControls(prev => !prev);
};

const onSelectedAudioTrackChange = (itemValue: string) => {
const onSelectedAudioTrackChange = (itemValue: string | number) => {
console.log('on audio value change ' + itemValue);
if (itemValue === 'none') {
setSelectedAudioTrack({
type: SelectedTrackType.DISABLED,
});
} else {
setSelectedAudioTrack({
type: SelectedTrackType.INDEX,
value: itemValue,
});
setSelectedAudioTrack({type: audioTracksSelectionBy, value: itemValue});
}
};

Expand Down Expand Up @@ -324,6 +326,7 @@ const _Overlay = forwardRef<VideoRef, Props>((props, ref) => {
audioTracks={audioTracks}
selectedAudioTrack={selectedAudioTrack}
onValueChange={onSelectedAudioTrackChange}
audioTracksSelectionBy={audioTracksSelectionBy}
/>
<TextTrackSelector
textTracks={textTracks}
Expand Down
3 changes: 2 additions & 1 deletion examples/basic/src/constants/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {SampleVideoSource} from '../types';
import {localeVideo} from '../assets';
import {Platform} from 'react-native';

// This constant allows to change how the sample behaves regarding to texts selection.
// This constant allows to change how the sample behaves regarding to audio and texts selection.
// You can change it to change how selector will use tracks information.
// by default, index will be displayed and index will be applied to selected tracks.
// You can also use LANGUAGE or TITLE
export const textTracksSelectionBy = SelectedTrackType.INDEX;
export const audioTracksSelectionBy = SelectedTrackType.INDEX;

export const isIos = Platform.OS === 'ios';

Expand Down

0 comments on commit c96f7d4

Please sign in to comment.