我尝试了来自 pub 的pepe_to_text包中的以下代码,但我只能录制 10 秒或更短的时间,并且暂停也不起作用
void _startListening() async {
await _speechToText.listen(
onResult: _onSpeechResult,
listenFor: Duration(minutes: 5),
pauseFor: Duration(seconds: 5),
);
setState(() {
_confidenceLevel = 0;
});
}
void _stopListening() async {
if (_wordsSpoken.isNotEmpty) {
try {
print(_wordsSpoken); // Update with the actual endpoint
await uploadTrans(_wordsSpoken);
print('Transcription sent to server.');
} catch (e) {
print('Error sending transcription to server: $e');
}
} else {
print("no word is detected");
}
await _speechToText.stop();
setState(() {});
}
.
我也有同样的问题。我正在使用安卓。当它开始听时,它会耐心等待最初的语音输入,但只要你说话并暂停一秒钟,它就会立即停止听。本质上是强迫用户连续说话而不喘气。 作为解决方法,您可以通过一些条件检查将侦听器放入循环中。当您循环时,也许您可以将响应附加在一起。
我还发现了一个建议,您可以修改本机 Android Java 代码以进行语音识别。我也没有尝试过:但想在丢失之前分享我的搜索结果之一。
这是可能有帮助的 ChatGPT 响应。但请记住它已经一年多了:
要调整 Android 上 Flutter 中语音转文本识别的暂停持续时间,通常需要配置 SpeechRecognitionOptions。 Flutter 没有对此功能的内置支持,但您可以利用平台通道与原生 Android 代码交互并相应地调整设置。
以下是实现此目的的一般方法:
定义平台通道:设置平台通道以在 Flutter 代码和原生 Android 代码之间进行通信。
实现本机代码:在您的 Android 项目中,实现必要的代码以将语音识别配置为所需的暂停持续时间。这通常涉及使用 Android 的 SpeechRecognizer 类并设置识别参数。
从 Flutter 调用本机代码:使用平台通道从 Flutter 代码中调用本机方法。
这是一个简化的示例:
在您的 Dart 代码中:
```dart
import 'package:flutter/services.dart';
class SpeechToTextService {
static const platform = MethodChannel('your_channel_name');
Future<void> setPauseDuration(int duration) async {
try {
await platform.invokeMethod('setPauseDuration', {'duration': duration});
} on PlatformException catch (e) {
print("Failed to set pause duration: '${e.message}'.");
}
}
}
```
在您的 Android 原生代码中:
```java
import android.speech.RecognitionListener;
import android.speech.SpeechRecognizer;
import android.os.Bundle;
public class SpeechToTextHandler {
private SpeechRecognizer speechRecognizer;
public void setPauseDuration(int duration) {
// Configure the SpeechRecognizer with the desired pause duration
// For example:
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {}
@Override
public void onBeginningOfSpeech() {}
@Override
public void onRmsChanged(float rmsdB) {}
@Override
public void onBufferReceived(byte[] buffer) {}
@Override
public void onEndOfSpeech() {}
@Override
public void onError(int error) {}
@Override
public void onResults(Bundle results) {}
@Override
public void onPartialResults(Bundle partialResults) {}
@Override
public void onEvent(int eventType, Bundle params) {}
});
}
}
```
请记住适当处理权限请求和错误情况。这是一个基本的轮廓;您需要对其进行调整以适应您的特定要求和项目结构。此外,为了稳健性,应添加错误处理和权限。