简体   繁体   中英

Text To Speech using Google Engine in Android

I'm developing a simple application in Android, i have a problem with TextToSpeech.

The application must pronounce the letters of the alphabet and numbers using TextToSpeech, but there's a problem, by default devices use PicoTTS I wish they were forced to use of the google tts engine.

How i can do this ?

My code:

@SuppressLint("NewApi")
public class LearnAlphabet extends Activity {
 Button howto, number, alphabet;
 public TextToSpeech tts;



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

  ActionBar actionBar = getActionBar();
  actionBar.setDisplayShowTitleEnabled(true);
  actionBar.setDisplayHomeAsUpEnabled(true);
  actionBar.setHomeButtonEnabled(true);
  actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0170a5")));

  GridView gridview = (GridView) findViewById(R.id.gridview);
  gridview.setAdapter(new NumberFunction(this));

  final String locale = getApplicationContext().getResources().getConfiguration().locale.getLanguage();



  tts = new TextToSpeech(getApplicationContext(), 
   new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status) {
     if(status != TextToSpeech.ERROR){      

    switch (locale.toString()) {
     case "it":
      tts.setLanguage(new Locale("it_IT"));
     break;

     case "en":
      tts.setLanguage(new Locale("en_US")); 
     break;

     case "es":
      tts.setLanguage(new Locale("es_ES"));
     break;

     case "de":
      tts.setLanguage(new Locale("de_NL"));
     break;

     case "ru":
      tts.setLanguage(new Locale("ru_RU"));
     break;

     default:
      tts.setLanguage(new Locale("en_US"));  
     break;
    }    

     }              
    }
  }); 
 }

and for each letter or number:

  tts.speak(getResources().getString(R.string.a), TextToSpeech.QUEUE_FLUSH, null);

**** EDIT ***** Error if i set : tts.setEngineByPackageName("com.google.android.tts")

12-11 15:52:04.954: E/AndroidRuntime(12203): FATAL EXCEPTION: main
12-11 15:52:04.954: E/AndroidRuntime(12203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.imparafacile/com.imparafacile.LearnAlphabet}: java.lang.NullPointerException
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2311)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.os.Looper.loop(Looper.java:137)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.app.ActivityThread.main(ActivityThread.java:5214)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at java.lang.reflect.Method.invoke(Method.java:525)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at dalvik.system.NativeStart.main(Native Method)
12-11 15:52:04.954: E/AndroidRuntime(12203): Caused by: java.lang.NullPointerException
12-11 15:52:04.954: E/AndroidRuntime(12203):    at com.imparafacile.LearnAlphabet.onCreate(LearnAlphabet.java:47)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.app.Activity.performCreate(Activity.java:5133)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-11 15:52:04.954: E/AndroidRuntime(12203):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)

An NPE is being thrown because the tts object is null at the time you call tts.setEngineByPackageName().

tts won't be created until onInit() returns successfully, so you should invoke tts.setEngineByPackageName() in onInit() :

String googleTtsPackage = "com.google.android.tts", picoPackage = "com.svox.pico";

tts = new TextToSpeech(getApplicationContext(), 
   new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status) {
     if(status != TextToSpeech.ERROR){
    if(!isPackageInstalled(getPackageManager(), googleTtsPackage)) 
    confirmDialog();  
  else myTTS.setEngineByPackageName(googleTtsPackage); 
   } 


 private void confirmDialog(){
           AlertDialog.Builder d = new AlertDialog.Builder(LearnAlphabet.this);
           d.setTitle("Install recommeded speech engine?");
           d.setMessage("Your device isn't using the recommended speech engine. Do you wish to install it?");
        d.setPositiveButton("Yes", new android.content.DialogInterface.OnClickListener(){
           @Override
        public void onClick(DialogInterface dialog, int arg1){
              Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installVoice);
        }});
    d.setNegativeButton("No, later", new android.content.DialogInterface.OnClickListener(){
           @Override
        public void onClick(DialogInterface dialog, int arg1){
               if(isPackageInstalled(context.getPackageManager(), picoPackage))
                    myTTS.setEngineByPackageName(picoPackage);

           }
    });
    d.show();
    }
      public static boolean isPackageInstalled(PackageManager pm, String packageName) {
            try {
                pm.getPackageInfo(packageName, 0);
            } catch (NameNotFoundException e) {
                return false;
            }
            return true;
    }

You can remove confirmDialog() if you aren't interested in redirecting users to install the google TTS engine if it isn't installed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM