SHARE
TWEET

Untitled

a guest Jun 19th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.recorder;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Locale;
  6.  
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.content.pm.PackageManager;
  10. import android.content.pm.ResolveInfo;
  11. import android.os.Bundle;
  12. import android.speech.RecognizerIntent;
  13. import android.speech.tts.TextToSpeech;
  14. import android.speech.tts.TextToSpeech.OnInitListener;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.widget.AdapterView;
  18. import android.widget.ArrayAdapter;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.ListView;
  22. import android.widget.Spinner;
  23.  
  24.  
  25. /**
  26.  *
  27.  * @author John
  28.  * Speech To Text(Text) - To Speech
  29.  */
  30.  
  31. public class TtS extends Activity implements OnInitListener, OnClickListener {
  32.  
  33.     private TextToSpeech mTts;
  34.     String textToSpeak = "No text Entered";
  35.     String language = "English";
  36.     int MY_DATA_CHECK_CODE = 0;
  37.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
  38.     private ListView mList;
  39.     private EditText input;
  40.  
  41.     @Override
  42.     public void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.main);
  45.         final Button button = (Button) findViewById(R.id.button01);
  46.         final Button speakButton = (Button) findViewById(R.id.button02);
  47.         mList = (ListView) findViewById(R.id.ListView01);
  48.         mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  49.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  50.                     long arg3) {
  51.                 input.setText((String) arg0.getItemAtPosition(arg2));
  52.             }
  53.           });
  54.         input = (EditText) findViewById(R.id.input);
  55.         Spinner lang = (Spinner) findViewById(R.id.language);
  56.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
  57.         this, R.array.langs, android.R.layout.simple_spinner_item);
  58.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  59.         lang.setAdapter(adapter);
  60.         lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  61.                     public void onItemSelected(AdapterView<?> parent,
  62.                             View view, int pos, long id) {
  63.                             Object item = parent.getItemAtPosition(pos);
  64.                             language = (String) item;
  65.                     }
  66.  
  67.                     public void onNothingSelected(AdapterView<?> parent) {
  68.                     }
  69.                 });
  70.         button.setOnClickListener(new View.OnClickListener() {
  71.             public void onClick(View v) {
  72.                 textToSpeak = input.getText().toString();
  73.                 if (textToSpeak.length() == 0)
  74.                     textToSpeak = "No text Entered.";
  75.                     speak();
  76.             }
  77.         });
  78.         // Check to see if a recognition activity is present
  79.         PackageManager pm = getPackageManager();
  80.         List<ResolveInfo> activities = pm.queryIntentActivities(
  81.                 new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
  82.         if (activities.size() != 0) {
  83.             speakButton.setOnClickListener(this);
  84.         } else {
  85.             speakButton.setEnabled(false);
  86.             speakButton.setText("Recognizer not present");
  87.         }
  88.         Intent checkIntent = new Intent();
  89.         checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
  90.         startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
  91.         mTts = new TextToSpeech(this, this);
  92.     }
  93.  
  94.     @Override
  95.     public void onInit(int arg0) {
  96.         // TODO Auto-generated method stub
  97.     }
  98.  
  99.     public Locale assignLang() {
  100.         if (language.equalsIgnoreCase("us english"))
  101.             return Locale.US;
  102.         if (language.equalsIgnoreCase("uk english"))
  103.             return Locale.UK;
  104.         if (language.equalsIgnoreCase("Italian"))
  105.             return Locale.ITALIAN;
  106.         if (language.equalsIgnoreCase("chinese"))
  107.             return Locale.CHINESE;
  108.         if (language.equalsIgnoreCase("german"))
  109.             return Locale.GERMAN;
  110.         if (language.equalsIgnoreCase("canadian"))
  111.             return Locale.CANADA;
  112.         if (language.equalsIgnoreCase("french-canadian"))
  113.             return Locale.CANADA_FRENCH;
  114.         if (language.equalsIgnoreCase("japenese"))
  115.             return Locale.JAPANESE;
  116.         if (language.equalsIgnoreCase("korean"))
  117.             return Locale.KOREAN;
  118.         if (language.equalsIgnoreCase("taiwanese"))
  119.             return Locale.TAIWAN;
  120.         return Locale.ENGLISH;
  121.     }
  122.  
  123.     public void speak() {
  124.         mTts.setLanguage(assignLang());
  125.         mTts.speak(textToSpeak, TextToSpeech.QUEUE_FLUSH, null);
  126.     }
  127.  
  128.     public void onClick(View v) {
  129.         if (v.getId() == R.id.button02) {
  130.             startVoiceRecognitionActivity();
  131.         }
  132.     }
  133.  
  134.     /**
  135.      * Fire an intent to start the speech recognition activity.
  136.      */
  137.     private void startVoiceRecognitionActivity() {
  138.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  139.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  140.                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  141.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
  142.         startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
  143.     }
  144.  
  145.     /**
  146.      * Handle the results from the recognition activity.
  147.      */
  148.     @Override
  149.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  150.         if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
  151.             // Fill the list view with the strings the recognizer thought it could have heard
  152.             ArrayList<String> matches = data.getStringArrayListExtra(
  153.                     RecognizerIntent.EXTRA_RESULTS);
  154.             mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
  155.                     matches));
  156.         }
  157.  
  158.         super.onActivityResult(requestCode, resultCode, data);
  159.     }
  160.  
  161.  
  162. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top