React Native - 无法同时使用语音转文本和文本转语音

问题描述 投票:0回答:2

我正在使用react-native-voice 和expo-speech 库来转录我的声音并将文本转换为语音。问题是,当我结束注册声音并使用 expo-voice 开始演讲时,没有声音。当录音结束时,react-native-voice 似乎完全静音了音频。演讲开始,但我必须按下麦克风按钮(激活语音识别)才能听到。

我发现让一切协同工作的唯一方法是在文本转语音结束后停止录音。这是代码的一部分:

const startRecognizing = async () => { setButtonColor('#38b000'); // Starts listening for speech for a specific locale try { await Voice.start('en-EN'); } catch (e) { console.error(e); } }; const destroyRecognizer = async () => { //Destroys the current SpeechRecognizer instance try { await Voice.destroy(); } catch (e) { console.error(e); } }; const stopRecognizing = async () => { setButtonColor('#344E41'); setTimeout(() => { Speech.speak("I did not understand, can you repeat please ?", { language: 'en-EN', onDone: () => { setTimeout( async() => { await destroyRecognizer(); }, 1000); }, }); }, 1000); };
  return (
    <View style={styles.microphoneButtonContainer}>
        <TouchableOpacity
        onPressIn={startRecognizing}
        onPressOut={stopRecognizing}>
        <Image
            style={styles.microphoneButton}
            source={require('../img/microphone-icon.png')}
            />
        </TouchableOpacity>
    </View>
    );
这个解决方案带来了很多边缘情况,所以我无法使用它。提供的停止录制的方法都不能解决问题。我在库文档中没有找到任何帮助。有针对这个的解决方法吗 ?谢谢您的帮助!

react-native text-to-speech speech-to-text
2个回答
0
投票
因为没有办法用react-native-voice方法解决这个问题,所以我就有了如果可以修改原生代码就直接去库里搜索的想法。对于 ios,代码位于 ios/voice/Voice.m 中。我发现了这个:

- (void) teardown { self.isTearingDown = YES; [self.recognitionTask cancel]; self.recognitionTask = nil; // Set back audio session category [self resetAudioSession];
所以我尝试注释掉

[self resetAudioSession];

,然后我用 npx-pod-install 重建了软件包(我使用 cocoapod),并且它起作用了!

这样做可能会导致边缘情况,我还没有完全测试这些方法,并且我没有尝试过android。


0
投票
您知道如何一起使用这两个库了吗?

我有同样的情况,但我无法弄清楚:(

© www.soinside.com 2019 - 2024. All rights reserved.