Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.recorder;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Locale;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.speech.tts.TextToSpeech;
- import android.speech.tts.TextToSpeech.OnInitListener;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Spinner;
- /**
- *
- * @author John
- * Speech To Text(Text) - To Speech
- */
- public class TtS extends Activity implements OnInitListener, OnClickListener {
- private TextToSpeech mTts;
- String textToSpeak = "No text Entered";
- String language = "English";
- int MY_DATA_CHECK_CODE = 0;
- private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
- private ListView mList;
- private EditText input;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final Button button = (Button) findViewById(R.id.button01);
- final Button speakButton = (Button) findViewById(R.id.button02);
- mList = (ListView) findViewById(R.id.ListView01);
- mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- input.setText((String) arg0.getItemAtPosition(arg2));
- }
- });
- input = (EditText) findViewById(R.id.input);
- Spinner lang = (Spinner) findViewById(R.id.language);
- ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
- this, R.array.langs, android.R.layout.simple_spinner_item);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- lang.setAdapter(adapter);
- lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- public void onItemSelected(AdapterView<?> parent,
- View view, int pos, long id) {
- Object item = parent.getItemAtPosition(pos);
- language = (String) item;
- }
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- textToSpeak = input.getText().toString();
- if (textToSpeak.length() == 0)
- textToSpeak = "No text Entered.";
- speak();
- }
- });
- // Check to see if a recognition activity is present
- PackageManager pm = getPackageManager();
- List<ResolveInfo> activities = pm.queryIntentActivities(
- new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
- if (activities.size() != 0) {
- speakButton.setOnClickListener(this);
- } else {
- speakButton.setEnabled(false);
- speakButton.setText("Recognizer not present");
- }
- Intent checkIntent = new Intent();
- checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
- startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
- mTts = new TextToSpeech(this, this);
- }
- @Override
- public void onInit(int arg0) {
- // TODO Auto-generated method stub
- }
- public Locale assignLang() {
- if (language.equalsIgnoreCase("us english"))
- return Locale.US;
- if (language.equalsIgnoreCase("uk english"))
- return Locale.UK;
- if (language.equalsIgnoreCase("Italian"))
- return Locale.ITALIAN;
- if (language.equalsIgnoreCase("chinese"))
- return Locale.CHINESE;
- if (language.equalsIgnoreCase("german"))
- return Locale.GERMAN;
- if (language.equalsIgnoreCase("canadian"))
- return Locale.CANADA;
- if (language.equalsIgnoreCase("french-canadian"))
- return Locale.CANADA_FRENCH;
- if (language.equalsIgnoreCase("japenese"))
- return Locale.JAPANESE;
- if (language.equalsIgnoreCase("korean"))
- return Locale.KOREAN;
- if (language.equalsIgnoreCase("taiwanese"))
- return Locale.TAIWAN;
- return Locale.ENGLISH;
- }
- public void speak() {
- mTts.setLanguage(assignLang());
- mTts.speak(textToSpeak, TextToSpeech.QUEUE_FLUSH, null);
- }
- public void onClick(View v) {
- if (v.getId() == R.id.button02) {
- startVoiceRecognitionActivity();
- }
- }
- /**
- * Fire an intent to start the speech recognition activity.
- */
- private void startVoiceRecognitionActivity() {
- 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 recognition demo");
- startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
- }
- /**
- * Handle the results from the recognition activity.
- */
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
- // Fill the list view with the strings the recognizer thought it could have heard
- ArrayList<String> matches = data.getStringArrayListExtra(
- RecognizerIntent.EXTRA_RESULTS);
- mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
- matches));
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement