我在我的应用中使用expo-speech
,但在某些iOS设备中未读取文本。通过让我的朋友测试它,我得到的结果是:
Works:
不工作:
[在本地开发的Ive尝试的每个iOS模拟器中,它也能正常工作。
我创建了一个非常简单的存储库(下面的链接),使用此代码对其进行测试:
import React from 'react';
import * as Speech from "expo-speech";
import { StyleSheet, Text, View } from 'react-native';
export default function App() {
setInterval(()=>{
Speech.speak(`I am a test`);
}, 2000);
return (
<View style={styles.container}>
<Text>Open up App.tsx to start working on your app!</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
https://snack.expo.io/@jamesweblondon/sound-test
使用Expo应用程序在iPhone 8 13.4.1上也无法正常工作。
更新:事实证明,这是由于静音模式引起的:https://github.com/expo/expo/issues/8235
理想的解决方案是像YouTube等一样使声音正常播放。
第二个最好的方法是在iOS中检测静默模式,这样我至少可以通知用户该功能将无法正常工作以及如何修复。
由于静音模式,我在workout-time应用程序上遇到了相同的问题。
Expo解决方案:
我们可以使用expo-av展览库通过playsInSilentModeIOS: true
在倾斜模式下播放语音>
import React, { useEffect } from "react"; import * as Speech from "expo-speech"; import { StyleSheet, Text, View } from "react-native"; import { Audio } from "expo-av"; export default function App() { useEffect(async () => { await Audio.setAudioModeAsync({ playsInSilentModeIOS: true }); // this will enable sound on silent mode setInterval(() => { Speech.speak(`I am a test`); }, 1000); }); return ( <View style={styles.container}> <Text>Open up App.tsx to start working on your app!</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: "#fff", alignItems: "center", justifyContent: "center", }, });
本机解决方案:
var Sound = require('react-native-sound');
Sound.setCategory('Playback'); // this will enable sound on silent mode