Using Speech Engine in Android

Posted by Unknown Kamis, 21 Maret 2013 0 komentar

This post is focusing on speech engine in Android SDK. The TTS engine is very important component of the Android SDK and the Android 4.0 version has great APIs to write the custom TTS engines.

Here i am explaining a sample code to use the TTS engine in your android application.

Create the following layout in your android application.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >

<TextView android:id="@+id/msglabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter some text in english:"
/>
<EditText android:id="@+id/inputtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/speakenglish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="speak in english"
/>

</LinearLayout>


Here is the code of the sample speak activity you can just copy and paste this in your application project.

package com.example.speaksample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.content.Intent;
import java.util.Locale;
import android.widget.Toast;

public class speaksample extends Activity implements OnClickListener, OnInitListener {
//Create the TTS object
private TextToSpeech TTSObject;
//status check code
private int DATA_CHECK_CODE = 0;
//create the Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speaking_android);
//get a reference to the button element listed in the XML layout
Button speakButton = (Button)findViewById(R.id.speakenglish);
//set the click listener
speakButton.setOnClickListener(this);
//check for TTS data
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, DATA_CHECK_CODE);
}
//respond to button clicks
public void onClick(View v) {
//get the text entered
EditText inputtext = (EditText)findViewById(R.id.inputtext);
String data = inputtext.getText().toString();
speakInEnglish(data);
}
//speak the user text
private void speakInEnglish(String speech) {
//speak straight away
TTSObject.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
//act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//the user has the necessary data - create the TTS
TTSObject = new TextToSpeech(this, this);
}
else {
//no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
//setup TTS
public void onInit(int initStatus) {
//check for successful instantiation
if (initStatus == TextToSpeech.SUCCESS) {
if(TTSObject.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
TTSObject.setLanguage(Locale.US);
}
else if (initStatus == TextToSpeech.ERROR) {
Toast.makeText(this, "TTS engine fails", Toast.LENGTH_LONG).show();
}
}
}


If you like the code please share your feedback.

Thanks
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Using Speech Engine in Android
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://apk-xda.blogspot.com/2013/03/using-speech-engine-in-android.html. Terima kasih sudah singgah membaca artikel ini.

0 komentar:

Posting Komentar

Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of apk xda.