-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathAudioManager.js
64 lines (56 loc) · 1.73 KB
/
AudioManager.js
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
import { Audio } from 'expo-av';
// import * as AssetUtils from 'expo-asset-utils';
import Assets from './Assets';
class AudioManager {
sounds = {};
playAsync = async (name, isLooping) => {
return;
if (name in this.sounds) {
const soundObject = this.sounds[name];
try {
await soundObject.setPositionAsync(0);
await soundObject.setIsLoopingAsync(isLooping);
await soundObject.playAsync();
} catch ({ message }) {
console.warn(`Error playing audio: ${message}`);
}
} else {
console.warn("Audio doesn't exist", name);
}
};
stopAsync = async name => {
if (name in this.sounds) {
const soundObject = this.sounds[name];
try {
await soundObject.stopAsync();
} catch ({ message }) {
console.warn(`Error stopping audio: ${message}`);
}
} else {
console.warn("Audio doesn't exist", name);
}
};
configureExperienceAudioAsync = async () => {
await Audio.setIsEnabledAsync(true);
// return Audio.setAudioModeAsync({
// allowsRecordingIOS: false,
// interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
// playsInSilentModeIOS: false,
// shouldDuckAndroid: true,
// interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
// playThroughEarpieceAndroid: true
// });
};
setupAudioAsync = async () => {
const keys = Object.keys(Assets.audio);
for (let key of keys) {
const item = Assets.audio[key];
this.sounds[key.split('.')[0]] = (await Audio.Sound.createAsync(
item,
)).sound;
}
};
setupAsync = () =>
Promise.all([this.configureExperienceAudioAsync(), this.setupAudioAsync()]);
}
export default new AudioManager();