giovedì 6 dicembre 2012

Text to Speech (TTS) con Pico

Una delle funzioni meno utilizzate (ma anche delle piu' interessanti) e' quella di poter utilizzare delle API per poter enunciare una stringa

Il motore di TTS di default su Android e' Pico e non sempre ci sono tutti i linguaggi installati ..quindi se si vuole che il telefono parli in inglese non ci sono problemi ma se si vuole una lingua un po' piu' esotica va scaricata a parte

Gli esempi su come usare Pico sono gia' inclusi negli esempi dei developer di Android quindi non c'e' molto da fare se non usare il sistema


La voce e' piuttosto metallica e robotica rispetto ad altri motori TTS ma c'e' da dire che i file sono estremamente compatti

un primo metodo per testare TTS e' il seguente ma di fatto non e' utile in casi reali di programmazione
---------------------------------------------------------------

package com.example.tts;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;


public class MainActivity extends Activity implements OnInitListener{

private TextToSpeech talker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
talker = new TextToSpeech(this, this);
say("Luca Innocenti");
}

public void say(String text2say){
    talker.speak(text2say, TextToSpeech.QUEUE_FLUSH, null);
   }

public void onInit(int status) {
//say("Luca Innocenti");
}

@Override
public void onDestroy() {
if (talker != null) {
talker.stop();
talker.shutdown();
}
super.onDestroy();
}
}
---------------------------------------------------------------

Il secondo metodo e' decisamente piu' interattivo
---------------------------------------------------------------

package com.example.tts2;

import java.util.Locale;

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

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {

private Button pulsante;
private EditText txtv;
private TextToSpeech tts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pulsante = (Button) findViewById(R.id.button1);
txtv = (EditText) findViewById(R.id.editText1);

tts = new TextToSpeech(this, this);

pulsante.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                speakOut();
            }

        });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
 
            int result = tts.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                pulsante.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

}

private void speakOut() {
// TODO Auto-generated method stub
String text = txtv.getText().toString();
 
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

}

}
--------------------------------------------------------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="66dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="Button" />

</RelativeLayout>

Nessun commento:

Posta un commento

Perche' investire su Unix

 Un libro trovato nel libero scambio alla Coop su cio' che poteva essere e non e' stato...interessante la storia su Unix del primo c...