Wednesday, 26 August 2015

Android Programming Tutorials 11 : Uploading Pictures,Music In Android

TextVoice or Speech:

In this TextVoice or Speech tutorial we are going to see how the hard coded data(TEXT) in our application can translate in to speech, Here we Go.....


OutPut Of the Tutorial:





Create Android Application:
  • File >> New >> Android Application
  • Enter Project name: TextVoiceApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

2.Copy and past the following code in the MainActivity.java

package com.ambilpursunil.newapp;

import java.util.Locale;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

                public class TextVoice extends Activity implements OnClickListener {
 static final String[] texts={
                   "what's up dude", "dude dude dude dude....","i can handle them"
                          };
                         TextToSpeech tts;

            @Override
             protected void onCreate(Bundle savedInstanceState) {
                        // TODO Auto-generated method stub
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.textvoice);
                        Button b = (Button) findViewById(R.id.bTextVoice);
                        b.setOnClickListener(this);
  tts = new TextToSpeech(TextVoice.this, new TextToSpeech.OnInitListener() {
                       
             @Override
             public void onInit(int status) {
                        // TODO Auto-generated method stub
                       if(status != TextToSpeech.ERROR){
                       tts.setLanguage(Locale.US);
                       }
                       }
                       });
                      }

            @Override
            protected void onPause() {
                       // TODO Auto-generated method stub
                      if(tts != null){
                      tts.stop();
                      tts.shutdown();
                      }
                     super.onPause();
                   }

            @Override
            public void onClick(View v) {
                      // TODO Auto-generated method stub
                     Random r = new Random();
                     String random = texts[r.nextInt(3)];
                     tts.speak(random, TextToSpeech.QUEUE_FLUSH, null);
            }
}

2.Make the change the of activity_mani.java  file as the below

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bTextVoice"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="VoiceText"></Button>

</LinearLayout>

3.Copy and past the code for Manifest.xml  file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ambilpursunil.newapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <activity 
       android:name="com.ambilpursunil.newapp.TextVoice"
       android:configChanges="keyboard|keyboardHidden|orientation>


   <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />

  </intent-filter>

       </activity>
  </application>
</manifest>

4.Right click on the project and Run As-->Android Application


           


Just click on the button and listen to the output what ever i written in the String array of MainActivity.java


Please Send Your Comments To ambilpura.sunil@gmail.com

Stay Tune For Next Tutorial... SQlite Database in Android:

No comments:

Post a Comment