Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.android.speechtest;
- import java.util.ArrayList;
- import java.util.Locale;
- import android.app.Activity;
- import android.app.Dialog;
- import android.content.ActivityNotFoundException;
- import android.content.Intent;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.speech.tts.TextToSpeech;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements
- TextToSpeech.OnInitListener {
- protected static final int RESULT_SPEECH = 1;
- private TextToSpeech tts;
- Toast toast;
- TextView textView;
- ArrayList<String> text;
- Button btnPlay, btnRe, btnExit;
- String localeSys;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- localeSys = Locale.getDefault().toString();
- Toast.makeText(getApplicationContext(),
- "System locale detected: " + localeSys, Toast.LENGTH_LONG)
- .show();
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, localeSys.trim());
- try {
- startActivityForResult(intent, RESULT_SPEECH);
- } catch (ActivityNotFoundException a) {
- Toast t = Toast.makeText(getApplicationContext(),
- "Opps! Your device doesn't support Speech to Text",
- Toast.LENGTH_LONG);
- t.show();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, 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) {
- text = data
- .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- Toast.makeText(getApplicationContext(), text.get(0),
- Toast.LENGTH_LONG).show();
- // Create custom dialog object
- final Dialog dialog = new Dialog(MainActivity.this);
- // Include dialog.xml file
- dialog.setContentView(R.layout.custom_dialog);
- // Set dialog title
- dialog.setTitle(text.get(0));
- TextView textV = (TextView) dialog.findViewById(R.id.txtDisp);
- textV.setText(text.get(0));
- // set values for custom dialog components - text, image and
- // button
- dialog.show();
- btnExit = (Button) dialog.findViewById(R.id.btnExit);
- btnExit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // Close dialog
- dialog.dismiss();
- System.exit(0);
- }
- });
- btnRe = (Button) dialog.findViewById(R.id.btnRestart);
- btnRe.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setClass(getApplicationContext(),
- MainActivity.class);
- startActivity(intent);
- }
- });
- btnPlay = (Button) dialog.findViewById(R.id.btnPlayText);
- btnPlay.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- speakOut(text.get(0));
- }
- });
- }
- break;
- }
- }
- }
- @Override
- public void onDestroy() {
- // Don't forget to shutdown tts!
- if (tts != null) {
- tts.stop();
- tts.shutdown();
- }
- super.onDestroy();
- }
- @Override
- public void onInit(int status) {
- if (status == TextToSpeech.SUCCESS) {
- int result = tts.setLanguage(Locale.US);
- if (result == TextToSpeech.LANG_MISSING_DATA
- || result == TextToSpeech.LANG_NOT_SUPPORTED) {
- Log.e("TTS", "This Language is not supported");
- } else {
- speakOut(text.get(0));
- }
- } else {
- Log.e("TTS", "Initilization Failed!");
- }
- }
- private void speakOut(String ttsText) {
- String text = ttsText;
- tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement