Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app.com.example.milad.speech;
- import android.content.Intent;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.speech.tts.TextToSpeech;
- import android.support.v7.app.AppCompatActivity;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.Locale;
- public class MainActivity extends AppCompatActivity {
- EditText edt_text;
- ImageView img_speak;
- ImageView img_speech;
- TextView txt_result;
- TextToSpeech textToSpeech;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- edt_text = (EditText) findViewById(R.id.edt_text);
- img_speak = (ImageView) findViewById(R.id.img_speak);
- img_speech = (ImageView) findViewById(R.id.img_speech);
- txt_result = (TextView) findViewById(R.id.txt_result);
- textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
- @Override
- public void onInit(int status) {
- if (status == TextToSpeech.SUCCESS) {
- int result = textToSpeech.setLanguage(Locale.US);
- if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
- Toast.makeText(MainActivity.this, "Language dosen't supported!", Toast.LENGTH_SHORT).show();
- img_speak.setEnabled(false);
- }
- }
- }
- });
- img_speak.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String text = edt_text.getText().toString().trim();
- if (!TextUtils.isEmpty(text)) {
- textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
- }
- }
- });
- img_speech.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);
- //set language
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "fa");
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech Recongize!");
- try {
- startActivityForResult(intent, 100);
- } catch (Exception e) {
- Toast.makeText(MainActivity.this, "Error , You're Device doesn't Support", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == 100) {
- if (resultCode == RESULT_OK && data != null) {
- ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- txt_result.setText(result.get(0));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement