Wednesday, May 18, 2011

Android Random BeatBox



Actualy it's a random generator with TextToSpeech.

For the BeatBox, refer YouTube video "Google Demo Slam: Translate Beat Box".

The random generated phase will be speaked using TextToSpeech, in GERMANY language if available.

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/generaterandom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate Random Text"
/>
<Button
android:id="@+id/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play Random Text"
/>
<EditText
android:id="@+id/beatbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android Random Beat Box"
/>
</LinearLayout>


AndroidBeatBox.java
package com.exercise.AndroidBeatBox;

import java.util.Locale;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidBeatBox extends Activity implements OnInitListener{

final static int NUM_TEXT = 25;
String[] beat = {"pv ",
"zk ",
"bschk ",
"kkkkkkkkkk ",
};

Button buttonGenerateRandomText, buttonPlay;
EditText RandomBeatBox;

TextToSpeech tts;
Random random;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonGenerateRandomText = (Button)findViewById(R.id.generaterandom);
buttonPlay = (Button)findViewById(R.id.play);
buttonPlay.setEnabled(false);
RandomBeatBox = (EditText)findViewById(R.id.beatbox);
random = new Random();
tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.GERMANY);

buttonGenerateRandomText.setOnClickListener(buttonGenerateRandomTextOnClick);
buttonPlay.setOnClickListener(buttonPlayOnClick);
}

private Button.OnClickListener buttonGenerateRandomTextOnClick
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String strBeat = "";
for(int i=0; i<=NUM_TEXT; i++){
int randomInt = random.nextInt(beat.length);
strBeat = strBeat + beat[randomInt];
}

RandomBeatBox.setText(strBeat);
}};

private Button.OnClickListener buttonPlayOnClick
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tts.speak(RandomBeatBox.getText().toString(),
TextToSpeech.QUEUE_ADD,
null);
}};

@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
buttonPlay.setEnabled(true);
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
tts.shutdown();
}
}


Download the files.



No comments:

Post a Comment