This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 57).
Expo 语音
一个提供文本转语音功能访问的库。
expo-speech 提供了一个 API,允许你在应用中使用文本转语音功能。
在 iOS 物理设备上,如果设备处于静音模式,expo-speech将不会发出声音。请确保静音模式已关闭。
安装
- npx expo install expo-speechIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
import { View, StyleSheet, Button } from 'react-native'; import * as Speech from 'expo-speech'; export default function App() { const speak = () => { const thingToSay = '1'; Speech.speak(thingToSay); }; return ( <View style={styles.container}> <Button title="按下以听一些词语" onPress={speak} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', backgroundColor: '#ecf0f1', padding: 8, }, });
API
import * as Speech from 'expo-speech';
Constants
Type: number
Maximum possible text length acceptable by Speech.speak() method. It is platform-dependent.
On iOS, this returns Number.MAX_VALUE.
Methods
Determine whether the Text-to-speech utility is currently speaking. Will return true if speaker
is paused.
Promise<boolean>Returns a Promise that fulfils with a boolean, true if speaking, false if not.
Pauses current speech. This method is not available on Android.
Promise<void>Resumes speaking previously paused speech or does nothing if there's none. This method is not available on Android.
Promise<void>| Parameter | Type | Description |
|---|---|---|
| text | string | The text to be spoken. Cannot be longer than |
| options(optional) | SpeechOptions | A Default: {} |
Speak out loud the text given options. Calling this when another text is being spoken adds an utterance to queue.
voidInterrupts current speech and deletes all in queue.
Promise<void>Types
| Parameter | Type |
|---|---|
| this | SpeechSynthesisUtterance |
| ev | SpeechSynthesisEvent |
any
| Property | Type | Description |
|---|---|---|
| _voiceIndex(optional) | number | - |
| language(optional) | string | The code of a language that should be used to read the |
| onBoundary(optional) | NativeBoundaryEventCallback | SpeechEventCallback | null | A callback that is invoked when the spoken utterance reaches a word. |
| onDone(optional) | () => void | SpeechEventCallback | A callback that is invoked when speaking finishes. |
| onError(optional) | (error: Error) => void | SpeechEventCallback | Only for: Android iOS A callback that is invoked when an error occurred while speaking. |
| onMark(optional) | SpeechEventCallback | null | - |
| onPause(optional) | SpeechEventCallback | null | - |
| onResume(optional) | SpeechEventCallback | null | - |
| onStart(optional) | () => void | SpeechEventCallback | A callback that is invoked when speaking starts. |
| onStopped(optional) | () => void | SpeechEventCallback | A callback that is invoked when speaking is stopped by calling |
| pitch(optional) | number | Pitch of the voice to speak |
| rate(optional) | number | Rate of the voice to speak |
| useApplicationAudioSession(optional) | boolean | Only for: iOS If you set this value to |
| voice(optional) | string | Voice identifier. |
| volume(optional) | number | Volume of the voice to speak Default: 1.0 |
Object describing the available voices on the device.
| Property | Type | Description |
|---|---|---|
| identifier | string | Voice unique identifier. |
| language | string | Voice language. |
| name | string | Voice name. |
| quality | VoiceQuality | Voice quality. |
Type: Voice extended by:
| Property | Type | Description |
|---|---|---|
| isDefault | boolean | - |
| localService | boolean | - |
| name | string | - |
| voiceURI | string | - |