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:
-----------------------------


Android Media Player Example

Android provides many ways to control playback of audio/video files and streams. One of this way is through a class called MediaPlayer
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">


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

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Pause"
       
android:id="@+id/button2"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="25dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Stop"
       
android:id="@+id/button3"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="25dp" />
</
LinearLayout>

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

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button
playbtn,pausebtn,stopbtn;
    MediaPlayer
mediaPlayer;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.a);
       
playbtn=(Button)findViewById(R.id.button);
       
pausebtn=(Button)findViewById(R.id.button2);
       
stopbtn=(Button)findViewById(R.id.button3);
       
playbtn.setOnClickListener(this);
       
pausebtn.setOnClickListener(this);
       
stopbtn.setOnClickListener(this);

    }

   
@Override
   
public void onClick(View v) {
       
switch (v.getId()){
           
case R.id.button:
               
mediaPlayer.start();
               
break;
           
case R.id.button2:
               
mediaPlayer.pause();
               
break;
           
case R.id.button3:
               
mediaPlayer.stop();
               
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.a);
               
break;
        }
    }

   
@Override
   
protected void onPause() {
       
mediaPlayer.pause();
       
super.onPause();
    }
}

Output :
-------------------------------


Encryption Decryption Example Android Using AES Alogirthm

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