Sunday, 1 November 2015

Speech to Text Example

Android comes with an inbuilt feature speech to text through which you can provide speech input to your app. With this you can add some of the cool features to your app like adding voice navigation(Helpful when you are targeting disabled people), filling a form with voice input etc.,
activity_main.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main"
   
tools:context=".MainActivity"
   
android:orientation="vertical">

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Speech to Text Example"
       
android:id="@+id/textView"
       
android:layout_gravity="center_horizontal" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Speak"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="10dp" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Display"
       
android:id="@+id/textView2"
       
android:layout_marginTop="10dp" />
</
LinearLayout>
 MainActivity.Java
------------------------------
package com.android.anil.speechtotext;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Button
speakbtn;
    TextView
display;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
speakbtn=(Button)findViewById(R.id.button);
       
display=(TextView)findViewById(R.id.textView2);
       
speakbtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                Intent intent=
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.
EXTRA_LANGUAGE_MODEL,"en_US");
               
try {
                    startActivityForResult(intent,
1);
                }
catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.
this, "Sorry Your Device does not support Speech to Text feautre", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

   
@Override
   
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       
if(requestCode==1){
        
if(resultCode==RESULT_OK&&data!=null) {
             ArrayList<String> al=data.getStringArrayListExtra(RecognizerIntent.
EXTRA_RESULTS);
            
display.setText(al.get(0));
         }
        }
       
super.onActivityResult(requestCode, resultCode, data);
    }
}
Output is:
-------------------
Note: You need to run it on the Real Device (example: Mobile) to Test the application .
           Android emulator does not support the speech to text feautre






Thursday, 29 October 2015

Text to Speech in Android

Android is providing a cool feature (from Android 1.6) called Text to Speech (TTS) which speaks the text in different languages. This tutorial explains how to work with android text to speech or android speech synthesis. In this tutorial i also explained changing the language type, pitch level and speed level.
activity_main.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main"
   
tools:context=".MainActivity"
   
android:orientation="vertical">

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText"
       
android:hint="enter mame"
       
android:layout_marginTop="5dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Speak"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="10dp" />
</
LinearLayout>

MainActivity.Java
-----------------------------
package com.android.anil.texttospeech;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button
speakbtn;
    EditText
name;
    TextToSpeech
textToSpeech;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
textToSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
           
@Override
           
public void onInit(int status) {
                Toast.makeText(MainActivity.
this, "Text to Speech engine is intialized", Toast.LENGTH_SHORT).show();
            }
        });
        
speakbtn=(Button)findViewById(R.id.button);
       
name=(EditText)findViewById(R.id.editText);
       
speakbtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                String s1=
name.getText().toString();
               
textToSpeech.speak(s1,RESULT_OK,null);
            }
        });
    }
}

Output is:
---------------------------

Wednesday, 28 October 2015

VideoView example in Android

In this tutorial, you will learn how to stream a remote video by using a MediaController and display it into a VideoView in your Android Application. MediaController is a controller for Media Player such as functions like Play/Pause, Rewind, Fast Forward and a progress slider.
activity_main.xml
----------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main"
   
tools:context=".MainActivity">


    <
VideoView
       
android:layout_width="wrap_content"
       
android:layout_height="match_parent"
       
android:id="@+id/videoView" />
</
LinearLayout>

MainActivity.Java
-----------------------------
package com.android.anil.videoplayer;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
VideoView
videoView;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
videoView=(VideoView)findViewById(R.id.videoView);
        
videoView.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.sample1));
       
videoView.setMediaController(new MediaController(this));
       
videoView.start();
    }
}

Output is:
-----------------------------


Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...