-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmodule.ts
343 lines (310 loc) · 10.3 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Importing modules
import {
NativeModules,
NativeEventEmitter,
Platform,
EmitterSubscription,
} from 'react-native';
import type {
AVAudioSessionCategory,
AVAudioSessionMode,
EmitterSubscriptionNoop,
RingMuteSwitchEventCallback,
RingerEventCallback,
RingerModeType,
setCheckIntervalType,
VolumeManagerSetVolumeConfig,
VolumeResult,
} from './types';
/**
* Error message when 'react-native-volume-manager' package is not linked properly
*/
const LINKING_ERROR =
`The package 'react-native-volume-manager' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo managed workflow\n';
/**
* Creates a proxy to throw an error when the module is not properly linked
*/
const VolumeManagerNativeModule = NativeModules.VolumeManager
? NativeModules.VolumeManager
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
/**
* Creates a proxy for the silent listener to throw an error when the module is not properly linked
*/
const SilentListenerNativeModule = NativeModules.VolumeManagerSilentListener
? NativeModules.VolumeManagerSilentListener
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
/**
* No operation emitter subscription
*/
const noopEmitterSubscription = {
remove: () => {
// noop
},
} as EmitterSubscriptionNoop;
/**
* Native event emitter for the Volume Manager
*/
const eventEmitter = new NativeEventEmitter(VolumeManagerNativeModule);
const silentEventEmitter = new NativeEventEmitter(SilentListenerNativeModule);
/**
* Checks if the current platform is Android
*/
const isAndroid = Platform.OS === 'android';
/**
* Returns the current ringer mode. Android only.
* @returns {Promise<RingerModeType | undefined>} - The current ringer mode or undefined if not Android.
*/
export async function getRingerMode(): Promise<RingerModeType | undefined> {
if (!isAndroid) {
return;
}
return VolumeManagerNativeModule.getRingerMode();
}
/**
* Sets the current device's ringer mode. Android only.
* @param {RingerModeType} mode - The ringer mode to set
* @returns {Promise<RingerModeType | undefined>} - The new ringer mode or undefined if not Android.
*/
export async function setRingerMode(
mode: RingerModeType
): Promise<RingerModeType | undefined> {
if (!isAndroid) {
return;
}
return VolumeManagerNativeModule.setRingerMode(mode);
}
/**
* iOS only. Enables or disables the audio session. When enabled, the session's category is set to ambient, allowing the audio from this session to mix with other audio currently playing on the device.
* @param {boolean} [enabled=true] - Whether to enable or disable the audio session.
* @param {boolean} [async=true] - Whether to perform the operation asynchronously. When set to true, this function will not block the UI thread.
* @returns {Promise<void>} - Resolves when the operation has finished. If an error occurs, it will be rejected with an instance of Error.
*/
export async function enable(
enabled: boolean = true,
async: boolean = true
): Promise<void> {
return VolumeManagerNativeModule.enable(enabled, async);
}
/**
* iOS only. Activates or deactivates the audio session. Does not change the audio session's category. When the session is deactivated, other audio sessions that had been interrupted by this one are reactivated and notified.
* @param {boolean} [value=true] - Whether to activate or deactivate the audio session.
* @param {boolean} [async=true] - Whether to perform the operation asynchronously. When set to true, this function will not block the JavaScript thread.
* @returns {Promise<void>} - Resolves when the operation has finished. If an error occurs, it will be rejected with an instance of Error. On Android, this function returns undefined.
*/
export async function setActive(
value: boolean = true,
async: boolean = true
): Promise<void> {
if (!isAndroid) {
return VolumeManagerNativeModule.setActive(value, async);
}
return undefined;
}
/**
* Sets the audio session category. iOS only.
* @param {AVAudioSessionCategory} value - The category to set
* @param {boolean} [mixWithOthers=false] - Allow audio to mix with others
* @returns {Promise<void>} - Resolves when the operation has finished
*/
export async function setCategory(
value: AVAudioSessionCategory,
mixWithOthers: boolean = false
): Promise<void> {
if (!isAndroid) {
return VolumeManagerNativeModule.setCategory(value, mixWithOthers);
}
return undefined;
}
/**
* Sets the audio session mode. iOS only.
* @param {AVAudioSessionMode} value - The mode to set
* @returns {Promise<void>} - Resolves when the operation has finished
*/
export async function setMode(value: AVAudioSessionMode): Promise<void> {
if (!isAndroid) {
return VolumeManagerNativeModule.setMode(value);
}
return undefined;
}
/**
* Enables or disables the VolumeManager in silent mode. iOS only.
* @param {boolean} [enabled=true] - Enable or disable the VolumeManager in silent mode
* @returns {Promise<void>} - Resolves when the operation has finished
*/
export async function enableInSilenceMode(
enabled: boolean = true
): Promise<void> {
if (isAndroid) {
return undefined;
}
return VolumeManagerNativeModule.enableInSilenceMode(enabled);
}
/**
* Checks if Do Not Disturb access is granted. Android only.
* @returns {Promise<boolean | undefined>} - Do Not Disturb access status or undefined if not Android.
*/
export async function checkDndAccess(): Promise<boolean | undefined> {
if (!isAndroid) {
return;
}
return VolumeManagerNativeModule.checkDndAccess();
}
/**
* Requests Do Not Disturb access. Android only.
* @returns {Promise<boolean | undefined>} - Do Not Disturb access request result or undefined if not Android.
*/
export async function requestDndAccess(): Promise<boolean | undefined> {
if (!isAndroid) {
return;
}
return VolumeManagerNativeModule.requestDndAccess();
}
/**
* Get the current device volume.
* @returns {Promise<VolumeResult>} - Returns a promise that resolves to an object with the volume value.
*/
export async function getVolume(): Promise<VolumeResult> {
return await VolumeManagerNativeModule.getVolume();
}
/**
* Set the current device volume.
* @param {number} value - The volume value to set. Must be between 0 and 1.
* @param {VolumeManagerSetVolumeConfig} [config={}] - Additional configuration for setting the volume.
* @returns {Promise<void>} - Resolves when the operation has finished
*/
export async function setVolume(
value: number,
config: VolumeManagerSetVolumeConfig = {}
): Promise<void> {
config = Object.assign(
{
playSound: false,
type: 'music',
showUI: false,
},
config
);
return await VolumeManagerNativeModule.setVolume(value, config);
}
/**
* Shows or hides the native volume UI.
* @param {object} config - An object with a boolean property 'enabled' to show or hide the native volume UI
* @returns {Promise<void>} - Resolves when the operation has
finished
*/
export async function showNativeVolumeUI(config: {
enabled: boolean;
}): Promise<void> {
return VolumeManagerNativeModule.showNativeVolumeUI(config);
}
/**
* Adds a listener for volume changes.
* @param {(result: VolumeResult) => void} callback - Function to be called when volume changes
* @returns {EmitterSubscription} - The subscription to the volume change event
*/
export function addVolumeListener(
callback: (result: VolumeResult) => void
): EmitterSubscription {
return eventEmitter.addListener('RNVMEventVolume', callback);
}
/**
* Adds a silent mode listener. iOS only.
* @param {RingMuteSwitchEventCallback} callback - Function to be called when silent mode changes
* @returns {EmitterSubscription | EmitterSubscriptionNoop} - The subscription to the silent mode change event
*/
export const addSilentListener = (
callback: RingMuteSwitchEventCallback
): EmitterSubscription | EmitterSubscriptionNoop => {
if (Platform.OS === 'ios') {
return silentEventEmitter.addListener('RNVMSilentEvent', callback);
}
return noopEmitterSubscription;
};
/**
* Sets the interval for the native silence check. iOS only.
* @param {number} value - The interval in milliseconds
*/
export const setNativeSilenceCheckInterval: setCheckIntervalType = (
value: number
) => {
if (Platform.OS === 'ios') {
SilentListenerNativeModule.setInterval(value);
}
};
/**
* Checks if the device is in a silent state (including silent mode, vibrate mode, or muted volume). Android only.
* @returns {Promise<boolean | null>} - Returns true if device is in a silent state, false otherwise, or null if not Android
*/
export const isAndroidDeviceSilent = (): Promise<boolean | null> => {
if (isAndroid) {
return SilentListenerNativeModule.isDeviceSilent();
}
return Promise.resolve(null);
};
/**
* Adds a ringer mode listener. Android only.
* @param {RingerEventCallback} callback - Function to be called when ringer mode changes
* @returns {EmitterSubscription | EmitterSubscriptionNoop} - The subscription to the ringer mode change event
*/
export const addRingerListener = (
callback: RingerEventCallback
): EmitterSubscription | EmitterSubscriptionNoop => {
if (isAndroid) {
SilentListenerNativeModule.registerObserver();
return silentEventEmitter.addListener('RNVMSilentEvent', callback);
}
return noopEmitterSubscription;
};
/**
* Removes a ringer mode listener. Android only.
* @param {EmitterSubscription | EmitterSubscriptionNoop} listener - The ringer mode listener to remove
*/
export const removeRingerListener = (
listener: EmitterSubscription | EmitterSubscriptionNoop
): void => {
if (isAndroid) {
SilentListenerNativeModule.unregisterObserver();
listener && listener.remove();
}
};
/**
* Exported object that includes all functions
*/
export const VolumeManager = {
addVolumeListener,
getVolume,
setVolume,
showNativeVolumeUI,
isAndroidDeviceSilent,
addSilentListener,
addRingerListener,
removeRingerListener,
setNativeSilenceCheckInterval,
getRingerMode,
setRingerMode,
checkDndAccess,
requestDndAccess,
enable,
setActive,
setCategory,
setMode,
enableInSilenceMode,
};
export default VolumeManager;