diff options
author | Santo Cariotti <santo@dcariotti.me> | 2025-01-17 20:38:11 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2025-01-17 20:38:11 +0100 |
commit | b87d072f715d1de50ba13abc233749f812b0be55 (patch) | |
tree | 11cfa229a18860fcca0e29bbbca180e09a01f0b2 | |
parent | 2a6848c679df7b3b39c275c0041fc5f8e02334f1 (diff) |
Get sound from binary string
-rw-r--r-- | app/(tabs)/index.tsx | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index d6b970b..cfae10f 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -148,23 +148,8 @@ export default function HomeScreen() { const [sound, setSound] = useState<Audio.Sound | null>(null); - async function playSound(alert: string, level: string) { - let levelDigit: string; - switch (level) { - case "ONE": - levelDigit = "1"; - break; - case "TWO": - levelDigit = "2"; - break; - case "THREE": - levelDigit = "3"; - break; - default: return; - } - const { sound } = await Audio.Sound.createAsync({ - uri: `${process.env.EXPO_PUBLIC_API_URL}/assets/sounds/alert-${alert}-text-${levelDigit}.mp3`, - }); + async function playSound(uri: any) { + const { sound } = await Audio.Sound.createAsync({ uri }); setSound(sound); await sound.playAsync(); } @@ -398,7 +383,7 @@ export default function HomeScreen() { "Content-Type": "application/json", }, body: JSON.stringify({ - query: `{ notifications(seen: false) { id, createdAt, level, alert { id, text1 text2 text3 }, movingActivity } }`, + query: `{ notifications(seen: false) { id, createdAt, level, alert { id, text1 text2 text3, audio1, audio2, audio3 }, movingActivity } }`, }), }, ); @@ -409,7 +394,27 @@ export default function HomeScreen() { const n = data.data.notifications[0]; setNotification(n); if (n.movingActivity == "IN_VEHICLE") { - playSound(n.alert.id, n.level); + let source = null; + switch (n.level) { + case "ONE": + source = n.alert.audio1; + break; + case "TWO": + source = n.alert.audio2; + break; + case "THREE": + source = n.alert.audio3; + break; + } + + if (source) { + let binary = ''; + source.forEach((byte: any) => { + binary += String.fromCharCode(byte); + }); + const uri = `data:audio/mpeg;base64,${btoa(binary)}` + playSound(uri); + } } } else { setNotification(null); |