Tuesday, October 2, 2012

Android Text to Speech | Tutorial Android

Android Text to Speech | Tutorial Android - Hello friend Panduan SEO | Panduan SEO terlengkap tuk pemula yang ingin mulai belajar SEO. Panduan gratis ini meningkatkan pengunjung ke situs anda. On this article you read this time with the title Android Text to Speech | Tutorial Android , We have prepared this article well for you to read and take the information in it. hopefully the contents of the post Articles Tutorial Android, That we write this you can understand. okay, happy reading.

Title : Android Text to Speech | Tutorial Android
links : Android Text to Speech | Tutorial Android

Read also


Android Text to Speech | Tutorial Android

Kali ini saya mau berbagi contoh aplikasi sederhana Text to Speech pada Android. Aplikasinya sederhana saja, jadi nanti aplikasinya mengeluarkan suara dari Text yang kita inputkan. Mau tau gimana cara membuatnya? Cekidott....

1. Buat project baru dengan nama "TextToSpeech" dengan nama activity "AndroidTextToSpeechActivity" dan layoutnya "main".

2. Masukkan kodingan berikut pada AndroidTextToSpeechActivity.java
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

public class AndroidTextToSpeechActivity extends Activity implements
        TextToSpeech.OnInitListener {
    /** Called when the activity is first created. */

    private TextToSpeech tts;
    private ImageButton btnSpeak;
    private EditText txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tts = new TextToSpeech(this, this);

        btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

        txtText = (EditText) findViewById(R.id.txtText);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {
        // TODO Auto-generated method stub

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            // tts.setPitch(5); // set pitch level

            // tts.setSpeechRate(2); // set speech speed rate

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed");
        }

    }

    private void speakOut() {

        String text = txtText.getText().toString();

        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}
 3. Selanjutnya pada main.xml, masukkan ....

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff">
   
    <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Text To Speech"
                android:padding="15dip"
                android:textColor="#000000"
                android:textSize="26dip"
                android:gravity="center"
                android:textStyle="bold"/>
   
    <EditText android:id="@+id/txtText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter some text to speak"
        android:layout_marginTop="20dip"       
        android:layout_margin="10dip"/>

    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/speak" />

</LinearLayout>

Sebelumnya buat dulu folder baru di res dengan nama drawable dan masukkan gambar ini (speak.png).


4. Habis itu coba RUN deh.... Kira-kira nanti keluarnya seperti ini,


Setelah itu coba masukkan text dan klik gambar speaker. Taraaa.... Ada suaranya kan? XD





Thus the article Android Text to Speech | Tutorial Android

That's an article Android Text to Speech | Tutorial Android This article will be useful for you all. okay, see you in other article posting. Do not forget to keep visiting on Panduan SEO | Blogger, Tips , Adsense, Google.

You are now reading the article Android Text to Speech | Tutorial Android with the link address https://belajarpanduanseo.blogspot.com/2012/10/android-text-to-speech-tutorial-android.html


Do not forget to share ^_^ because sharing is caring and I'm waiting for your comment




EmoticonEmoticon