Membuat Text to Speech pada Android
kali ini saya akan mencoba menjelaskan pembuatan aplikasi text to speech pada android
perhatikan langkah-langkah berikut :
Teknologi Text-to-Speech ini di peruntukkan kepada para penderita
tunanetra agar bisa mudah menjalankan computer. Namun bukan hanya
computer saja yang bisa melakukan hal ini, karena saat ini sudah bisa di
lakukan dalam perangkat android seperti memiliki buku bahasa yang bisa
membantu anda mempelajari bahasa baru.
Langkah pertama buat kelas MainActivity.
masukkan 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");
}
}
}
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");
}
}
}
Source code untuk file activity_main.xml
<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"
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: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:text="Clear" />
<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:text="Speak" />
</RelativeLayout>
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"
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: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:text="Clear" />
<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:text="Speak" />
</RelativeLayout>
Tampilan Emulator:
Komentar
Posting Komentar