Making a message in Android Studio under a condition - android

i would like to make a message under a condition if the pronounciation is correct according to google assistant,
if my speaking is correct then there will be a message sayin' that my pronounciation is correct
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = etText.getText().toString();
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
});
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mic_google = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mic_google.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mic_google.putExtra(RecognizerIntent.EXTRA_LANGUAGE, ID_BahasaIndonesia);
try {
startActivityForResult(mic_google, RESULT_SPEECH);
tvText.setText("");
}catch (ActivityNotFoundException e){
Toast.makeText(getApplicationContext(), "Maaf, Device Kamu Tidak Support Speech To Text", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case RESULT_SPEECH:
if (resultCode == RESULT_OK && data != null){
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
tvText.setText(text.get(0));
}
break;
}
}
}
here's my design now

Related

Android: SpeechRecognition not working

I'm working on the speech recognition in my application and I have 2 commands Command A and Command B, Suppose if i click on the mic and speak Command A it has to start Activity A or Activity B is Command is said.
In the below code START_ACTIVITY1 IS MY COMMAND A
protected static final int RESULT_SPEECH_START_ACTIVITYA=1;
protected static final int RESULT_SPEECH_START_ACTIVITYB=2;
mic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mic.setBackgroundResource(R.drawable.micactive);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH_START_ACTIVITYA);
// manInactiveButton.setBackgroundResource(R.drawable.man_inactive);
} catch (ActivityNotFoundException e) {
Toast t = Toast.makeText(getApplicationContext(), "Oops, Your device doesn't support Speech to Text", Toast.LENGTH_LONG);
t.show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH_START_ACTIVITYA:{
if (resultCode == RESULT_SPEECH_START_ACTIVITYA && null != data) {
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Intent mainIntent= new Intent(First_Screen.this, Second.class);
startActivity(mainIntent);
}
}
}
}
public class VoiceRecognition implements RecognitionListener, Runnable
{
#Override
public void run()
{
recognizer = SpeechRecognizer.createSpeechRecognizer(yourContext);
recognizer.setRecognitionListener((RecognitionListener) this);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//... all the intent stuff ...
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
recognizer.startListening(intent);
}
#Override
public void onResults(Bundle results)
{
ArrayList<String> matches;
matches=results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
}
}
....
VoiceRecognition voiceRecognizer = new VoiceRecognition();
Handler loopHandler = new Handler(Looper.getMainLooper());
loopHandler.post(voiceRecognizer);

Android speech recognition for non-dictionary words

I am developing an Android application of speech recognition for non-dictionary words. I want to remove the dictionary and do it for any word.
For example If I pronounce word "pini", my current application get it as "beanie". I want the word to recognize as it is pronounced.
My code:
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnSpeak:
promptSpeechInput();
break;}
}
#Override
// act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
} else {
Intent installTTSIntent = new Intent();
installTTSIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
// Receiving speech input
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);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
#Override
// act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
myTTS = new TextToSpeech(this, this);
} else {
Intent installTTSIntent = new Intent();
installTTSIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
// Receiving speech input
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);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
public void speakWords(String speech) {
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
/**
* Showing google speech input dialog
* */
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_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
Any help would be appreciated.
Thanks.

Newer show activity for ACTION_RECOGNIZE_SPEECH

I have this code:
/**
* Showing google speech input dialog
*/
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_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getActivity().getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
and i'm doing call:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
});
but i never seen activity with voice recording. Immediately there is a call:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == Activity.RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
mAdapter.addTag(result.get(0));
//txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
with resultCode == Activity.RESULT_CANCEL
I have permission to Internet.
Thank you.

Android Offline Speech Recognition to Display Only Word

I need help with android speech-text. Is it possible to display only the first word that was detected?like when the user inputs "the cat is playing" then the text that will only appear on the text box is the word "the".
The code that I used:
protected static final int RESULT_SPEECH = 1;
private Button btn_mic;
private TextView txtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_level1);
txtText = (TextView) findViewById(R.id.txt);
btn_mic = (Button) findViewById(R.id.mic);
btn_mic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
}
thanks in advance. :)
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(text.size()>0){
String firstOne=text.get(0);
if(firstOne.contains(" ")){
String firstWord=firstOne.substring(0, firstOne.indexOf(' '));
txtText.setText(text.get(0));
}else{
txtText.setText(text.get(0));
}
}

My activity closes down when i cancel or accept bluetooth enable request?

There is no error in the logcat just the current activity closes down and previous activity opens.
Here is the code where am puttin bluetooth enabling request:
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!(bluetooth.isEnabled())) {
Log.e("blue",""+"not working");
status = "Bluetooth is not Enabled.";
Toast.makeText(AddUser.this, status, Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
else
{
scand();
}
}
});
Here is onactivityresult:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println(resultCode);
Log.e("resultblue",""+resultCode);
if (resultCode == RESULT_CANCELED) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(AddUser.this, "Error Enabling bluetooth", Toast.LENGTH_LONG).show();
}
});
} else {
scand();
}
}
what could be the problem?

Categories

Resources