Last active
May 3, 2025 04:50
-
-
Save oovz/71b07d4beb6a85e7e3f86e65052f65fc to your computer and use it in GitHub Desktop.
expo-speech getAvailableVoicesAsync problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Text, SafeAreaView, StyleSheet, ActivityIndicator, View } from 'react-native'; | |
import React, { useState, useEffect } from 'react'; | |
import * as Speech from 'expo-speech'; | |
export default function App() { | |
const [voicesNumber, setVoicesNumber] = useState(null); | |
const [isLoading, setIsLoading] = useState(true); | |
useEffect(() => { | |
const fetchNumber = async () => { | |
try { | |
const voices = await Speech.getAvailableVoicesAsync(); | |
setVoicesNumber(voices.length); | |
} catch (error) { | |
console.error("Error fetching voices.length:", error); | |
setVoicesNumber('Error'); | |
} finally { | |
setIsLoading(false); | |
} | |
}; | |
fetchNumber(); | |
}, []); | |
return ( | |
<SafeAreaView style={styles.container}> | |
<View style={styles.loadingContainer}> | |
{isLoading ? ( | |
<ActivityIndicator size="large" color="#0000ff" /> | |
) : ( | |
<Text style={styles.numberDisplay}> | |
Voice length is: {voicesNumber} | |
</Text> | |
)} | |
</View> | |
</SafeAreaView> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: 'center', | |
backgroundColor: '#ecf0f1', | |
padding: 8, | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment