Saturday, January 19, 2013

android develop sample code–TTS android.settings.TextToSpeechSetting

1.android tts code here to call com.android.settings.TextToSpeechSetting:

Intent intent = new Intent();
intent.setAction("android.speech.tts.engine.CHECK_TTS_DATA");
intent.setClassName("com.android.settings", "com.android.settings.TextToSpeechSettings");
PkrssActivity.s_pkrss.startActivity(intent);



or

public static void openApp(String packageName,String className) {
try{
if(className.length()==0){
PackageInfo pi = PkrssActivity.s_pkrss.getPackageManager().getPackageInfo(packageName, 0);

Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);

List<ResolveInfo> apps = PkrssActivity.s_pkrss.getPackageManager().queryIntentActivities(resolveIntent, 0);

ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
packageName = ri.activityInfo.packageName;
className = ri.activityInfo.name;
}
}

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

ComponentName cn = new ComponentName(packageName, className);

intent.setComponent(cn);
PkrssActivity.s_pkrss.startActivity(intent);
}catch(Exception e){
e.printStackTrace();
}
}



2.check tts is installed

Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeechBeta.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);

it will return the callback in this function:

protected  void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_TTS_STATUS_CHECK)
{
switch (resultCode) {
case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_PASS:
//ok
{
mTTS = new TextToSpeechBeta(this, this);
Log.v(TAG, "TTS Engine is installed!");

}

break;
case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_BAD_DATA:
case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_MISSING_DATA:
case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
{
//in here,need redownload data.
Log.v(TAG, "Need language stuff:"+resultCode);
Intent dataIntent = new Intent();
dataIntent.setAction(TextToSpeechBeta.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(dataIntent);
}
break;
case TextToSpeechBeta.Engine.CHECK_VOICE_DATA_FAIL:
// check failed
default:
Log.v(TAG, "Got a failure. TTS apparently not available");
break;
}
}
else
{
// other Intent return result
}
}





 


3.install tts from web url apk:


download system tts lanauage package data:

Intent dataIntent = new Intent();
dataIntent.setAction(TextToSpeechBeta.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(dataIntent);



or download from other third part lanauage code from web url:


private void alertInstallEyesFreeTTSData()
{
Builder alertInstall = new AlertDialog.Builder(this)
.setTitle("lost voice engine lanauage package")
.setMessage("download tts lanauage package")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//download eyes-free tts lanauage package
String ttsDataUrl = "http://eyes-free.googlecode.com/files/tts_3.1_market.apk";
Uri ttsDataUri = Uri.parse(ttsDataUrl);
Intent ttsIntent = new Intent(Intent.ACTION_VIEW, ttsDataUri);
startActivity(ttsIntent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
alertInstall.create().show();

}




4. instance the tts object.

No comments: