MediaPlayer and Text to Speech : MediaPlayer « Media « Android






MediaPlayer and Text to Speech

   
package app.test;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Test extends Activity implements OnInitListener {
  private EditText words = null;
  private Button speakBtn = null;
  private EditText filename = null;
  private Button recordBtn = null;
  private Button playBtn = null;
  private EditText useWith = null;
  private Button assocBtn = null;
  private String soundFilename = null;
  private File soundFile = null;
    private static final int REQ_TTS_STATUS_CHECK = 0;
  private static final String TAG = "TTS Demo";
    private TextToSpeech mTts = null;
    private MediaPlayer player = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        words = (EditText)findViewById(R.id.wordsToSpeak);
        filename = (EditText)findViewById(R.id.filename);
        useWith = (EditText)findViewById(R.id.realText);

        speakBtn = (Button)findViewById(R.id.speakBtn);
        recordBtn = (Button)findViewById(R.id.recordBtn);
        playBtn = (Button)findViewById(R.id.playBtn);
        assocBtn = (Button)findViewById(R.id.assocBtn);
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
    }
    
    public void doButton(View view) {
      switch(view.getId()) {
      case R.id.speakBtn:
          mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
          break;
      case R.id.recordBtn:
        soundFilename = filename.getText().toString();
        soundFile = new File(soundFilename);
        if (soundFile.exists())
          soundFile.delete();

          if(mTts.synthesizeToFile(words.getText().toString(), null, soundFilename)== TextToSpeech.SUCCESS) {
            Toast.makeText(getBaseContext(),"Sound file created",Toast.LENGTH_SHORT).show();
            playBtn.setEnabled(true);
            assocBtn.setEnabled(true);
          }
          else {
            Toast.makeText(getBaseContext(),"Oops! Sound file not created",Toast.LENGTH_SHORT).show();
          }
          break;
      case R.id.playBtn:
        try {
            player = new MediaPlayer();
                player.setDataSource(soundFilename);
          player.prepare();
            player.start();
        }
        catch(Exception e) {
            Toast.makeText(getBaseContext(),"Hmmmmm. Can't play file",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
        break;
      case R.id.assocBtn:
        mTts.addSpeech(useWith.getText().toString(), soundFilename);
          Toast.makeText(getBaseContext(),"Associated!",Toast.LENGTH_SHORT).show();
          break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQ_TTS_STATUS_CHECK) {
            switch (resultCode) {
            case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
                mTts = new TextToSpeech(this, this);
                Log.v(TAG, "Pico is installed okay");
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
                Intent installIntent = new Intent();
                installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
            default:
                Log.e(TAG, "Got a failure. TTS apparently not available");
            }
        }
    }

  public void onInit(int status) {
    if( status == TextToSpeech.SUCCESS) {
      speakBtn.setEnabled(true);
      recordBtn.setEnabled(true);
    }
  }
  @Override
  public void onPause()
  {
    super.onPause();
        if(player != null) {
          player.stop();
        }
    if( mTts != null)
      mTts.stop();
  }

  @Override
    public void onDestroy()
  {
        super.onDestroy();
        if(player != null) {
          player.release();
        }
    if( mTts != null) {
      mTts.shutdown();
    }
    }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <EditText android:id="@+id/wordsToSpeak"
    android:text="Dohn Keyhotay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <Button android:id="@+id/speakBtn"
    android:text="Speak"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="doButton"
  android:enabled="false" />

  <TextView android:id="@+id/filenameLabel"
    android:text="Filename:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

  <EditText android:id="@+id/filename"
    android:text="/sdcard/donquixote.wav"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <Button android:id="@+id/recordBtn"
    android:text="Record"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="doButton"
  android:enabled="false" />

  <Button android:id="@+id/playBtn"
    android:text="Play"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="doButton"
  android:enabled="false" />

  <TextView android:id="@+id/useWithLabel"
    android:text="Use with:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

  <EditText android:id="@+id/realText"
    android:text="Don Quixote"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  <Button android:id="@+id/assocBtn"
    android:text="Associate"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:onClick="doButton"
  android:enabled="false" />

</LinearLayout>

   
    
    
  








Related examples in the same category

1.Using MediaPlayer to play MP3 file
2.Load mp3 file with MediaPlayer and play
3.Call prepare for MediaPlayer before starting
4.Set URL data source for MediaPlayer
5.Audio MediaPlayer Demo
6.Using MediaPlayer to play Video and Audio
7.extends MediaPlayer
8.MediaPlayer create and start
9.Multi PlayMixer
10.Play movie.m4v file
11.Using Service to play media file