SHARE
TWEET

Untitled

a guest Jun 12th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package app.com.example.milad.speech;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.speech.RecognizerIntent;
  6. import android.speech.tts.TextToSpeech;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.text.TextUtils;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.ImageView;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. import java.util.ArrayList;
  15. import java.util.Locale;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19.   EditText edt_text;
  20.   ImageView img_speak;
  21.   ImageView img_speech;
  22.   TextView txt_result;
  23.   TextToSpeech textToSpeech;
  24.  
  25.   @Override
  26.   protected void onCreate(Bundle savedInstanceState) {
  27.     super.onCreate(savedInstanceState);
  28.     setContentView(R.layout.activity_main);
  29.  
  30.     edt_text = (EditText) findViewById(R.id.edt_text);
  31.     img_speak = (ImageView) findViewById(R.id.img_speak);
  32.     img_speech = (ImageView) findViewById(R.id.img_speech);
  33.     txt_result = (TextView) findViewById(R.id.txt_result);
  34.  
  35.     textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
  36.       @Override
  37.       public void onInit(int status) {
  38.         if (status == TextToSpeech.SUCCESS) {
  39.           int result = textToSpeech.setLanguage(Locale.US);
  40.           if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  41.             Toast.makeText(MainActivity.this, "Language dosen't supported!", Toast.LENGTH_SHORT).show();
  42.             img_speak.setEnabled(false);
  43.           }
  44.         }
  45.       }
  46.     });
  47.  
  48.     img_speak.setOnClickListener(new View.OnClickListener() {
  49.       @Override
  50.       public void onClick(View v) {
  51.         String text = edt_text.getText().toString().trim();
  52.  
  53.         if (!TextUtils.isEmpty(text)) {
  54.           textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  55.         }
  56.       }
  57.     });
  58.  
  59.     img_speech.setOnClickListener(new View.OnClickListener() {
  60.       @Override
  61.       public void onClick(View v) {
  62.  
  63.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  64.  
  65.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  66.  
  67.         //set language
  68.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "fa");
  69.  
  70.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech Recongize!");
  71.         try {
  72.           startActivityForResult(intent, 100);
  73.  
  74.         } catch (Exception e) {
  75.           Toast.makeText(MainActivity.this, "Error , You're Device doesn't Support", Toast.LENGTH_SHORT).show();
  76.         }
  77.       }
  78.     });
  79.   }
  80.  
  81.   @Override
  82.   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  83.     if (requestCode == 100) {
  84.       if (resultCode == RESULT_OK && data != null) {
  85.         ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
  86.         txt_result.setText(result.get(0));
  87.  
  88.       }
  89.     }
  90.   }
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top