AIDE Tutorial - 29 Text To Speech
Watch the video
Main.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:textSize="35dp"
android:text="Text To Speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:textColorHint="#D80E18"
android:textColor="#45EA1F"
android:layout_marginTop="45dp"
android:id="@+id/mainEditText"
android:hint="EnterText here"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/mainButton"
android:text="Speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"/>
</LinearLayout>
MainActivity.Java
package com.kc.tts;
import android.app.*;
import android.os.*;
import android.speech.tts.*;
import android.widget.*;
import java.util.*;
import android.view.View.*;
import android.view.*;
public class MainActivity extends Activity
{TextToSpeech tts; EditText et; Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText)findViewById(R.id.mainEditText);
btn=(Button)findViewById(R.id.mainButton);
tts=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener()
{@Override public void onInit(int status)
{if (status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);}}});
btn.setOnClickListener(new View.OnClickListener()
{@Override public void onClick(View v){
String toSpeak= et.getText().toString();
tts.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);}});}}
0 comments:
Post a Comment