Android ACTION_RECOGNIZE_SPEECH 意图在长篇大论之后永远不会结束
Android ACTION_RECOGNIZE_SPEECH intent never finishes after long speech
我开始打算并等待结果。它在简短的演讲中效果很好,但如果演讲太长,它不会给我答案。 (将近 1 分钟)
final Intent searchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
searchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "tr");
searchIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, true);
searchIntent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, true);
startActivityForResult(searchIntent, VOICE_REQUEST_CODE);
除了 SpeechRecognizer 之外,还有其他方法可以从 ACTION_RECOGNIZE_SPEECH 意图中获取结果吗?
试试这个 google 文本到语音意图启动器,
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Now");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, REQUEST_CODE);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent,REQUEST_CODE);
希望这对您有所帮助:)
这是一个可行的解决方案:
final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, yourPackageHere);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1000);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Your Prompt");
startActivityForResult(intent,REQUEST_CODE);
但在使用此功能之前,您应该检查用户是否授予 RECORD_AUDIO
权限以及设备是否有 ACTION_RECOGNIZE_SPEECH
可用。
识别语音对长语音有一个有趣的行为。如果您给 MAX_RESULTS
一个小数字,识别语音屏幕会在长时间语音后冻结。所以你需要保持数字更大,你会从识别语音意图中得到 onActivityResult
和 List<String> results
的结果。您可以通过循环获得结果,而不是使用。
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SPEECH_INPUT =1000 ;
//views from activity
TextView mTextTv;
ImageButton mVoiceBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
mTextTv= findViewById( R.id.textTv );
mVoiceBtn=findViewById( R.id.voiceBtn );
//button clic to show speech to text dilog
mVoiceBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
} );
}
private void speak() {
//intent is show speech to text dialog
Intent intent= new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH );
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault() );
intent.putExtra( RecognizerIntent.EXTRA_PROMPT, "HI speak something" );
// Start intent
try {
//in there was no errror
//show dilog
startActivityForResult( intent, REQUEST_CODE_SPEECH_INPUT);
}
catch(Exception e){
//show messageof error and show
Toast.makeText( this,""+e.getMessage(), Toast.LENGTH_SHORT ).show();
}
}
//recive voice input and handle it
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult( requestCode, resultCode, data );
switch (requestCode){
case REQUEST_CODE_SPEECH_INPUT:{
if(resultCode==RESULT_OK && null!=data){
//get text arry form voice intent
ArrayList<String> result=data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS );
//set to text view
mTextTv.setText( result.get( 0 ) );
}
break;
}
}
}
}
我开始打算并等待结果。它在简短的演讲中效果很好,但如果演讲太长,它不会给我答案。 (将近 1 分钟)
final Intent searchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
searchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "tr");
searchIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, true);
searchIntent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, true);
startActivityForResult(searchIntent, VOICE_REQUEST_CODE);
除了 SpeechRecognizer 之外,还有其他方法可以从 ACTION_RECOGNIZE_SPEECH 意图中获取结果吗?
试试这个 google 文本到语音意图启动器,
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Now");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, REQUEST_CODE);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
startActivityForResult(intent,REQUEST_CODE);
希望这对您有所帮助:)
这是一个可行的解决方案:
final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, yourPackageHere);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1000);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Your Prompt");
startActivityForResult(intent,REQUEST_CODE);
但在使用此功能之前,您应该检查用户是否授予 RECORD_AUDIO
权限以及设备是否有 ACTION_RECOGNIZE_SPEECH
可用。
识别语音对长语音有一个有趣的行为。如果您给 MAX_RESULTS
一个小数字,识别语音屏幕会在长时间语音后冻结。所以你需要保持数字更大,你会从识别语音意图中得到 onActivityResult
和 List<String> results
的结果。您可以通过循环获得结果,而不是使用。
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_SPEECH_INPUT =1000 ;
//views from activity
TextView mTextTv;
ImageButton mVoiceBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
mTextTv= findViewById( R.id.textTv );
mVoiceBtn=findViewById( R.id.voiceBtn );
//button clic to show speech to text dilog
mVoiceBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
} );
}
private void speak() {
//intent is show speech to text dialog
Intent intent= new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH );
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault() );
intent.putExtra( RecognizerIntent.EXTRA_PROMPT, "HI speak something" );
// Start intent
try {
//in there was no errror
//show dilog
startActivityForResult( intent, REQUEST_CODE_SPEECH_INPUT);
}
catch(Exception e){
//show messageof error and show
Toast.makeText( this,""+e.getMessage(), Toast.LENGTH_SHORT ).show();
}
}
//recive voice input and handle it
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult( requestCode, resultCode, data );
switch (requestCode){
case REQUEST_CODE_SPEECH_INPUT:{
if(resultCode==RESULT_OK && null!=data){
//get text arry form voice intent
ArrayList<String> result=data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS );
//set to text view
mTextTv.setText( result.get( 0 ) );
}
break;
}
}
}
}