![](/img/trans.png)
[英]How to add Speech-to-Text into a chat application that is written on React Native
[英]Using text to speech in react native
我正在嘗試開發一個將文本翻譯成語音的本機應用程序......但無法弄清楚將我的 TextInput 元素的輸入鏈接到執行翻譯的函數的方法..
我將在這里分享我的代碼
const HomeScreen = ({ navigation }) => {
const speak = () => {
let thingToSay = "i will try to remember";
Speech.speak(thingToSay);
};
return (
<View style={styles.screen}>
<ImageBackground
source={require("./assets/book.png")}
style={styles.backgroundImage}
>
<View style={styles.innerText}>
<Text style={styles.textDisplay}>
Type the sentence you want to practice
</Text>
<View>
<TextInput placeholder="HERE" />
</View>
<Button title="TRANSLATE" onPress={speak} />
{/* <Button title="SEND" /> */}
</View>
</ImageBackground>
</View>
);
};
這是你的方法。
添加狀態管理以保存文本並添加方法以更新 textInputChange 上的狀態。
const HomeScreen = ({ navigation }) => {
const [textToSpeak, setTextToSpeak] = useState('');
const speak = () => {
Speech.speak(textToSpeak);
};
const _onChangeText = text => setTextToSpeak(text);
return (
<View style={styles.screen}>
<ImageBackground
source={require("./assets/book.png")}
style={styles.backgroundImage}
>
<View style={styles.innerText}>
<Text style={styles.textDisplay}>
Type the sentence you want to practice
</Text>
<View>
<TextInput
placeholder="HERE"
onChangeText={_onChangeText}
/>
</View>
<Button title="TRANSLATE" onPress={speak} />
{/* <Button title="SEND" /> */}
</View>
</ImageBackground>
</View>
);
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.