Expo-speech 在某些 iOS 设备上不工作
Expo-speech not working on some iOS devices
我在我的应用程序中使用 expo-speech
,但在某些 iOS 设备中无法读出文本。
让我的朋友测试它,我得到的结果是:
作品:
- iPhone X.13.3.1
- iPhone10.
- iPhone X.13.3.1
- iPhone 8 加。 13.3.1
- iPhone 7+。 13.1.2
不工作:
- iPhone Xs。 13.3.1
- iPhone 8. 13.4.1
它在我在本地开发时尝试过的每个 iOS 模拟器中也工作正常。
我创建了一个非常简单的存储库(下面的 link)以使用以下代码对其进行测试:
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-av expo 库在 playsInSilentModeIOS: true
的静音模式下播放语音
当其他声音处于播放模式时,在 expo 文本到语音中工作。所以你可以附加空声音
从下载空音 here
import React, { useEffect } from "react";
import * as Speech from "expo-speech";
import { StyleSheet, Text, View, Platform } from "react-native";
import { Audio } from "expo-av";
const soundObject = new Audio.Sound();
export default function App() {
useEffect(() => {
const enableSound = async () => {
if (Platform.OS === "ios") {
await Audio.setAudioModeAsync({
playsInSilentModeIOS: true,
});
await soundObject.loadAsync(require("./soundFile.mp3"));
await soundObject.playAsync();
}
setInterval(() => {
Speech.speak(`I am a test`);
}, 1000);
};
enableSound();
});
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",
},
});
React-Native 解决方案:
var Sound = require('react-native-sound');
Sound.setCategory('Playback'); // this will enable sound on silent mode
我在我的应用程序中使用 expo-speech
,但在某些 iOS 设备中无法读出文本。
让我的朋友测试它,我得到的结果是:
作品:
- iPhone X.13.3.1
- iPhone10.
- iPhone X.13.3.1
- iPhone 8 加。 13.3.1
- iPhone 7+。 13.1.2
不工作:
- iPhone Xs。 13.3.1
- iPhone 8. 13.4.1
它在我在本地开发时尝试过的每个 iOS 模拟器中也工作正常。
我创建了一个非常简单的存储库(下面的 link)以使用以下代码对其进行测试:
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-av expo 库在 playsInSilentModeIOS: true
当其他声音处于播放模式时,在 expo 文本到语音中工作。所以你可以附加空声音
从下载空音 here
import React, { useEffect } from "react";
import * as Speech from "expo-speech";
import { StyleSheet, Text, View, Platform } from "react-native";
import { Audio } from "expo-av";
const soundObject = new Audio.Sound();
export default function App() {
useEffect(() => {
const enableSound = async () => {
if (Platform.OS === "ios") {
await Audio.setAudioModeAsync({
playsInSilentModeIOS: true,
});
await soundObject.loadAsync(require("./soundFile.mp3"));
await soundObject.playAsync();
}
setInterval(() => {
Speech.speak(`I am a test`);
}, 1000);
};
enableSound();
});
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",
},
});
React-Native 解决方案:
var Sound = require('react-native-sound');
Sound.setCategory('Playback'); // this will enable sound on silent mode