Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.asusx441n.latihantexttospeech;
- import java.util.Locale;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.SharedPreferences.Editor;
- import android.speech.tts.TextToSpeech;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- String text;
- EditText et;
- TextToSpeech tts;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- et=(EditText) findViewById(R.id.edit_Text1);
- tts=new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
- @Override
- public void onInit(int status) {
- // TODO Auto-generated method stub
- if(status == TextToSpeech.SUCCESS){
- final Locale localeID = new Locale("in", "ID");
- int result=tts.setLanguage(localeID);
- if(result==TextToSpeech.LANG_MISSING_DATA ||
- result==TextToSpeech.LANG_NOT_SUPPORTED){
- Log.e("error", "This Language is not supported");
- }
- else{
- ConvertTextToSpeech();
- }
- }
- else
- Log.e("error", "Initilization Failed!");
- }
- });
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- if(tts != null){
- tts.stop();
- tts.shutdown();
- }
- super.onPause();
- }
- public void onClick(View v){
- ConvertTextToSpeech();
- }
- private void ConvertTextToSpeech() {
- // TODO Auto-generated method stub
- text = et.getText().toString();
- if(text==null||"".equals(text))
- {
- text = "Kontent tidak tersedia";
- tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
- }else
- tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement