1 | import ExponentAV from './ExponentAV';
|
2 |
|
3 | export * from './Audio/Recording';
|
4 | export * from './Audio/Sound';
|
5 | export { setIsEnabledAsync } from './Audio/AudioAvailability';
|
6 | export { PitchCorrectionQuality } from './AV';
|
7 |
|
8 | export type AudioMode = {
|
9 | allowsRecordingIOS: boolean;
|
10 | interruptionModeIOS: number;
|
11 | playsInSilentModeIOS: boolean;
|
12 | staysActiveInBackground: boolean;
|
13 | interruptionModeAndroid: number;
|
14 | shouldDuckAndroid: boolean;
|
15 | playThroughEarpieceAndroid: boolean;
|
16 | };
|
17 |
|
18 | export const INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS = 0;
|
19 | export const INTERRUPTION_MODE_IOS_DO_NOT_MIX = 1;
|
20 | export const INTERRUPTION_MODE_IOS_DUCK_OTHERS = 2;
|
21 |
|
22 | export const INTERRUPTION_MODE_ANDROID_DO_NOT_MIX = 1;
|
23 | export const INTERRUPTION_MODE_ANDROID_DUCK_OTHERS = 2;
|
24 |
|
25 |
|
26 | const _isValueValid = (value: any, validValues: any[]): boolean => {
|
27 | return validValues.filter(validValue => validValue === value).length > 0;
|
28 | };
|
29 |
|
30 | const _populateMissingKeys = (
|
31 | userAudioMode: Partial<AudioMode>,
|
32 | defaultAudioMode: AudioMode
|
33 | ): AudioMode => {
|
34 | for (const key in defaultAudioMode) {
|
35 | if (!userAudioMode.hasOwnProperty(key)) {
|
36 | userAudioMode[key] = defaultAudioMode[key];
|
37 | }
|
38 | }
|
39 | return userAudioMode as AudioMode;
|
40 | };
|
41 |
|
42 | const defaultMode: AudioMode = {
|
43 | allowsRecordingIOS: false,
|
44 | interruptionModeIOS: INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS,
|
45 | playsInSilentModeIOS: false,
|
46 | staysActiveInBackground: false,
|
47 | interruptionModeAndroid: INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
|
48 | shouldDuckAndroid: true,
|
49 | playThroughEarpieceAndroid: true,
|
50 | };
|
51 |
|
52 | let currentAudioMode: AudioMode | null = null;
|
53 |
|
54 | function getCurrentAudioMode(): AudioMode {
|
55 | if (!currentAudioMode) {
|
56 | return defaultMode;
|
57 | }
|
58 | return currentAudioMode;
|
59 | }
|
60 |
|
61 | export async function setAudioModeAsync(partialMode: Partial<AudioMode>): Promise<void> {
|
62 | const mode = _populateMissingKeys(partialMode, getCurrentAudioMode());
|
63 |
|
64 | if (
|
65 | !_isValueValid(mode.interruptionModeIOS, [
|
66 | INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS,
|
67 | INTERRUPTION_MODE_IOS_DO_NOT_MIX,
|
68 | INTERRUPTION_MODE_IOS_DUCK_OTHERS,
|
69 | ])
|
70 | ) {
|
71 | throw new Error(`"interruptionModeIOS" was set to an invalid value.`);
|
72 | }
|
73 | if (
|
74 | !_isValueValid(mode.interruptionModeAndroid, [
|
75 | INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
|
76 | INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
|
77 | ])
|
78 | ) {
|
79 | throw new Error(`"interruptionModeAndroid" was set to an invalid value.`);
|
80 | }
|
81 | if (
|
82 | typeof mode.allowsRecordingIOS !== 'boolean' ||
|
83 | typeof mode.playsInSilentModeIOS !== 'boolean' ||
|
84 | typeof mode.staysActiveInBackground !== 'boolean' ||
|
85 | typeof mode.shouldDuckAndroid !== 'boolean' ||
|
86 | typeof mode.playThroughEarpieceAndroid !== 'boolean'
|
87 | ) {
|
88 | throw new Error(
|
89 | '"allowsRecordingIOS", "playsInSilentModeIOS", "playThroughEarpieceAndroid", "staysActiveInBackground" and "shouldDuckAndroid" must be booleans.'
|
90 | );
|
91 | }
|
92 | currentAudioMode = mode;
|
93 | return await ExponentAV.setAudioMode(mode);
|
94 | }
|