Android文本到语音和语音到文本(Android Text to speech And Speech to text)
我正在同时处理文本到语音和语音到文本。 我正在制作一个应用程序,通过文本到语音提问,并通过语音和应用程序从用户获得答案将其转换为文本。 但它不能正常工作。 两者都在同时工作,就像它说的那样,它发回信息。 我们可以给予一些延迟,以便当它停止说话时,它会听取声音并返回该文本。 `
import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.speech.RecognizerIntent; import android.speech.tts.TextToSpeech; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Locale; public class MainActivity extends Activity implementsTextToSpeech.OnInitListener { TextView eText1; TextToSpeech textToSpeech; String speech = "Hey, Can u read me?"; private final int REQ_CODE_SPEECH_INPUT = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); eText1 = (TextView)findViewById(R.id.textView2); textToSpeech = new TextToSpeech(this,this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQ_CODE_SPEECH_INPUT: { if (resultCode == RESULT_OK && null != data) { ArrayList<String> result = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); eText1.setText(result.get(0)); } break; } } } private void promptSpeechInput() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, speech); try { startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); } catch (ActivityNotFoundException a) { Toast.makeText(getApplicationContext(), getString(R.string.speech_not_supported), Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void speakOut() { String text = speech; textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null); } @Override public void onInit(int status) { if(status == TextToSpeech.SUCCESS){ int result = textToSpeech.setLanguage(Locale.ENGLISH); if(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){ Toast.makeText(this, "This language is not supported", Toast.LENGTH_LONG).show(); } else{ speakOut(); promptSpeechInput(); } }else{ Toast.makeText(this, "Initialization failed", Toast.LENGTH_LONG).show(); } } }
`
I am working with text to speech and speech to text at the same time. I am making an app in which it ask a question through text to speech and get the answer from the user through speech and app convert it to text. but it does not work fine. both are working at the same time like what it speak, it text it back. can we give some delay so that when it stop speaking then it listen for the voice and return that text. `
import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.speech.RecognizerIntent; import android.speech.tts.TextToSpeech; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Locale; public class MainActivity extends Activity implementsTextToSpeech.OnInitListener { TextView eText1; TextToSpeech textToSpeech; String speech = "Hey, Can u read me?"; private final int REQ_CODE_SPEECH_INPUT = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); eText1 = (TextView)findViewById(R.id.textView2); textToSpeech = new TextToSpeech(this,this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQ_CODE_SPEECH_INPUT: { if (resultCode == RESULT_OK && null != data) { ArrayList<String> result = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); eText1.setText(result.get(0)); } break; } } } private void promptSpeechInput() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, speech); try { startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); } catch (ActivityNotFoundException a) { Toast.makeText(getApplicationContext(), getString(R.string.speech_not_supported), Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void speakOut() { String text = speech; textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null); } @Override public void onInit(int status) { if(status == TextToSpeech.SUCCESS){ int result = textToSpeech.setLanguage(Locale.ENGLISH); if(result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){ Toast.makeText(this, "This language is not supported", Toast.LENGTH_LONG).show(); } else{ speakOut(); promptSpeechInput(); } }else{ Toast.makeText(this, "Initialization failed", Toast.LENGTH_LONG).show(); } } }
`
原文:https://stackoverflow.com/questions/29569282
最满意答案
您不能,ID和类未提交,因此在PHP中不可用。 您可以做的是通过将结果转换为数组来获得相同名称的结果。 将
Form::text('answer1',
更改为Form::text('answer1[]'
以便您可以提交具有相同名称的多个输入。如果你然后使用
$input['answer_body'] = Input::get('answer1');
,$input['answer_body']
将包含一个数组。 您可以使用点表示法来指定要获取的Input::get('answer1.0');
,例如:Input::get('answer1.0');
将获取名称为answer1[]
的第一个输入中的值。You can't, the ID and class aren't submitted and so aren't available in the PHP. What you can do is get results by the same name by turning them into an array. Change
Form::text('answer1',
toForm::text('answer1[]'
so that you can submit multiple inputs with the same name.If you then use
$input['answer_body'] = Input::get('answer1');
,$input['answer_body']
will have an array in it. You can get specific answers by using dot notation to specify which input you want to get, e.g.:Input::get('answer1.0');
will fetch the value in the first input with a name ofanswer1[]
.
相关问答
更多-
如何在laravel的下拉列表中显示id和value?(how to display both id and value in dropdown list in laravel?)[2023-11-22]
尝试这个: 在控制器中 $supplier_list = Supplier::lists('supplier_name', 'id')->get(); 在模板中