首页 > 解决方案 > Expo-speech 在某些 iOS 设备上不起作用

问题描述

expo-speech在我的应用程序中使用,但在某些 iOS 设备中没有读出文本。从让我的朋友测试它,我得到的结果是:

作品:

不工作:

它也适用于我在本地开发时尝试过的每个 iOS 模拟器。

我创建了一个非常简单的 repo(下面的链接)来使用以下代码对其进行测试:

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 中检测静音模式,这样我至少可以通知用户此功能不起作用以及如何修复它。

标签: expotext-to-speech

解决方案


由于静音模式,我在锻炼时间应用程序上遇到了同样的问题。

世博解决方案:

我们可以使用expo-av expo 库在静默模式下播放语音playsInSilentModeIOS: true

当其他声音处于播放模式时,在世博会文本到语音的工作。所以你可以附加空的声音

从这里下载空的声音

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

推荐阅读