Advertisement
euis_kusesa

MainActivity.java

Dec 27th, 2018
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package com.example.asusx441n.latihantexttospeech;
  2.  
  3. import java.util.Locale;
  4.  
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.content.SharedPreferences.Editor;
  8. import android.speech.tts.TextToSpeech;
  9. import android.util.Log;
  10. import android.view.Menu;
  11. import android.view.View;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14.  
  15. public class MainActivity extends Activity {
  16.  
  17.     String text;
  18.     EditText et;
  19.     TextToSpeech tts;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.  
  26.         et=(EditText) findViewById(R.id.edit_Text1);
  27.         tts=new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
  28.  
  29.             @Override
  30.             public void onInit(int status) {
  31.                 // TODO Auto-generated method stub
  32.                 if(status == TextToSpeech.SUCCESS){
  33.                     final Locale localeID = new Locale("in", "ID");
  34.                     int result=tts.setLanguage(localeID);
  35.                     if(result==TextToSpeech.LANG_MISSING_DATA ||
  36.                             result==TextToSpeech.LANG_NOT_SUPPORTED){
  37.                         Log.e("error", "This Language is not supported");
  38.                     }
  39.                     else{
  40.                         ConvertTextToSpeech();
  41.                     }
  42.                 }
  43.                 else
  44.                     Log.e("error", "Initilization Failed!");
  45.             }
  46.         });
  47.     }
  48.  
  49.     @Override
  50.     protected void onPause() {
  51.         // TODO Auto-generated method stub
  52.  
  53.         if(tts != null){
  54.  
  55.             tts.stop();
  56.             tts.shutdown();
  57.         }
  58.         super.onPause();
  59.     }
  60.  
  61.     public void onClick(View v){
  62.  
  63.         ConvertTextToSpeech();
  64.  
  65.     }
  66.  
  67.     private void ConvertTextToSpeech() {
  68.         // TODO Auto-generated method stub
  69.         text = et.getText().toString();
  70.         if(text==null||"".equals(text))
  71.         {
  72.             text = "Kontent tidak tersedia";
  73.             tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  74.         }else
  75.             tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement