Cara Buat Aplikasi Android Text To Speech
Ini Source Code Untuk MainActivity.java
package com.example.texttospeach;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
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.EditText;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnClickListener{
EditText input;
Button button_clear, button_speak;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.editText1);
button_clear = (Button) findViewById(R.id.button1);
button_speak = (Button) findViewById(R.id.button2);
button_clear.setOnClickListener(this);
button_speak.setOnClickListener(this);
tts = new TextToSpeech(this, this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
input.setText("");
break;
case R.id.button2:
String text = input.getText().toString();
if (text.isEmpty()) {
Toast.makeText(MainActivity.this, "Text is empty", Toast.LENGTH_SHORT).show();
} else {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
break;
}
}
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
Locale bahasa = tts.getLanguage();
int result = tts.setLanguage(bahasa);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS","this language is not supported");
} else {
}
} else {
Log.e("TTS", "initialization failed");
}
}
}
Setelah Itu Kita Buatkan File Layout activity_main.xml ini source codenya
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/text"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:hint="Input English Text"
android:textColor="#ffffffff"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="28dp"
android:layout_marginTop="44dp"
android:background="@drawable/clean"
android:text="" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText1"
android:layout_marginRight="33dp"
android:background="@drawable/speaker"
android:text="" />
Dan ini adalah hasil running akhir dari source code di atas
Komentar
Posting Komentar