gpt4 book ai didi

android - 如何构建 BufferReceived() 以使用 RecognizerIntent 捕获语音?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:04 26 4
gpt4 key购买 nike

我正在使用 android 应用程序 RecognizerIntent.ACTION_RECOGNIZE_SPEECH,,, 我的问题是我不知道如何 创建将捕获用户输入的语音的缓冲区。一世 阅读了很多关于堆栈溢出的内容,但我只是不明白如何 我会将缓冲区和识别服务调用返回到我的代码中。以及我将如何播放保存到缓冲区中的内容。

这是我的代码:

       public class Voice extends Activity implements OnClickListener {
byte[] sig = new byte[500000] ;
int sigPos = 0 ;
ListView lv;
static final int check =0;
protected static final String TAG = null;

@Override
protected void onCreate(Bundle savedInstanceState) {



// TODO Auto-generated method stub
super.onCreate(savedInstanceState);


setContentView(R.layout.voice);

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
"com.domain.app");

SpeechRecognizer recognizer = SpeechRecognizer
.createSpeechRecognizer(this.getApplicationContext());

RecognitionListener listener = new RecognitionListener() {

@Override
public void onResults(Bundle results) {
ArrayList<String> voiceResults = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (voiceResults == null) {
Log.e(TAG, "No voice results");
} else {
Log.d(TAG, "Printing matches: ");
for (String match : voiceResults) {
Log.d(TAG, match);
}
}
}

@Override
public void onReadyForSpeech(Bundle params) {
Log.d(TAG, "Ready for speech");
}

@Override
public void onError(int error) {
Log.d(TAG,
"Error listening for speech: " + error);
}

@Override
public void onBeginningOfSpeech() {
Log.d(TAG, "Speech starting");
}

@Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
TextView display=(TextView)findViewById (R.id.Text1);
display.setText("True");


System.arraycopy(buffer, 0, sig, sigPos, buffer.length) ;
sigPos += buffer.length ;

}

@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub

}

@Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub

}

@Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub

}

@Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub

}
};
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);




startActivityForResult(intent,check);

}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}



}

最佳答案

Android 语音识别 API(从 API 级别 17 开始)不提供可靠的音频捕获方式。

您可以使用“接收到缓冲区”回调,但请注意

RecognitionListener说到 onBufferReceived:

More sound has been received. The purpose of this function is to allow giving feedback to the user regarding the captured audio. There is no guarantee that this method will be called.

buffer: a buffer containing a sequence of big-endian 16-bit integers representing a single channel audio stream. The sample rate is implementation dependent.

RecognitionService.Callback说到 bufferReceived:

The service should call this method when sound has been received. The purpose of this function is to allow giving feedback to the user regarding the captured audio.

buffer: a buffer containing a sequence of big-endian 16-bit integers representing a single channel audio stream. The sample rate is implementation dependent.

所以此回调用于关于捕获音频的反馈,不一定是捕获音频本身,即可能是出于可视化目的的简化版本。此外,“不能保证会调用此方法”,即 Google 语音搜索可能会在 v1 中提供它,但随后决定在 v2 中将其删除。

另请注意,在识别过程中可以多次调用此方法。但是,如果缓冲区代表完整的录制音频或仅代表自上次调用以来的 fragment ,则没有记录。 (我假设是后者,但您需要使用语音识别器对其进行测试。)

因此,在您的实现中,您应该将缓冲区复制到一个全局变量中以进行保存,例如识别完成后转换为 wav 文件。

关于android - 如何构建 BufferReceived() 以使用 RecognizerIntent 捕获语音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16339519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com