java - Speak failed: tts engine connection not fully setup in the TextToSpeech functionality -


package com.example.trynot;  import java.io.ioexception; import java.util.calendar; import java.util.locale; import java.util.properties; import java.util.logging.level; import java.util.logging.logger;  import javax.mail.address; import javax.mail.bodypart; import javax.mail.flags; import javax.mail.folder; import javax.mail.message; import javax.mail.messagingexception; import javax.mail.multipart; import javax.mail.nosuchproviderexception; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.store; import javax.mail.flags.flag; import javax.mail.search.flagterm;  import com.example.trynot.mainactivity; import com.example.trynot.r;  import android.annotation.suppresslint; import android.app.activity; import android.app.activitymanager; import android.app.activitymanager.runningserviceinfo; import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.database.sqlite.sqlitedatabase; import android.os.asynctask; import android.os.bundle; import android.os.handler; import android.sax.startelementlistener; import android.speech.tts.texttospeech; import android.speech.tts.texttospeech.oninitlistener; import android.support.v4.app.notificationcompat; import android.support.v4.app.notificationcompat.builder; import android.util.log; import android.view.menu; import android.view.menuitem; import android.widget.toast; import android.speech.tts.texttospeech;  public class mainactivity extends activity {     private static final int my_data_check_code = 1234;     public static context c,b;     public texttospeech tts;     public static intent serviceintent;     private static int mynotificationid;       public static class  readmailsample extends asynctask<string,string,void> implements texttospeech.oninitlistener{         message message;         private texttospeech tts;         static string command, phonenumber, type, priority, name, time_stamp, imei, opt1, opt2, opt3, fromsubstring;         properties properties = null;         private session session = null;         private store store = null;         private folder inbox = null;         string username="avarote1994@gmail.com" ;                   // provide recepient email id         string password="amul11111994" ;                            // provide recepient password         static sqlitedatabase db;         boolean flag=false;         context acn;         //private bundle savedinstancestate;           protected void doinbackground(string...params){                     // separate thread run in background             try{                 readmails();             }              catch(exception e){                 logger logger = logger.getanonymouslogger();                 logger.log(level.info, "an exception thrown", e);             }             return null;         }          readmailsample(sqlitedatabase db){         this.db = db;       }        readmailsample(){        }        readmailsample(context cn){          acn=cn;       }       @override       protected void onpreexecute() {            super.onpreexecute();            tts = new texttospeech(c,readmailsample.this);           }        @override       protected void onprogressupdate(string... values) {             try {                  system.out.println("---------------------adasd-----------" + time_stamp);                  shownotification();                   speakout();             }             catch(exception e){                 e.printstacktrace();             }                     }          @override       public void oninit(int status) {        if (status == texttospeech.success) {           tts.speak(command, texttospeech.queue_flush, null);           int result = tts.setlanguage(locale.us);            if (result == texttospeech.lang_missing_data                   || result == texttospeech.lang_not_supported) {               log.e("tts", "this language not supported");           } else {                speakout();           }        } else {           log.e("tts", "initilization failed!");       }        }         public void speakout() {           tts.speak("hello hi", texttospeech.queue_flush, null);           }              public void shownotification() {             pendingintent notificationintent = preparependingintent();             notification notification = createbasicnotification(notificationintent);             displaynotification(notification);         }           @suppresslint("inlinedapi")         private pendingintent preparependingintent() {             intent intent=new intent(c,mainactivity.class);              intent.setflags(intent.flag_activity_new_task | intent.flag_activity_clear_task);              pendingintent pendingintent = pendingintent.getactivity(                     c,                      0,                      intent,                      pendingintent.flag_update_current);             return pendingintent;         }          private notification createbasicnotification(pendingintent notificationintent) {             notificationcompat.builder builder = new builder(c);             long[] vibrationpattern = {0, 200, 800, 200, 600, 200, 400, 200, 200, 200, 100, 200, 50, 200, 50, 200, 50, 200, 50, 200};               notification notification = builder                     .setsmallicon(r.drawable.ic_launcher)                     .setcontenttitle("medication reminder")                     .setcontenttext(command)                     .setautocancel(true)                     .setcontentintent(notificationintent)                     .setwhen(calendar.getinstance().gettimeinmillis() + 1000*60+60)                     .setvibrate(vibrationpattern)                      .build();              return notification;         }           private void displaynotification(notification notification) {              notificationmanager notificationmanager = (notificationmanager)c.getsystemservice(context.notification_service);             mynotificationid=(int) system.currenttimemillis();             notificationmanager.notify(mynotificationid , notification);          }             public void readmails() throws ioexception{           system.out.println("readmail hi");               properties = new properties();            // setting imap server access recepient's email           properties.setproperty("mail.host", "imap.gmail.com");           properties.setproperty("mail.port", "995");           properties.setproperty("mail.transport.protocol", "imaps");           while(true){// continuously monitor incoming mail's               //string cq = "select * login4";               //cursor c = db.rawquery(cq, null);               //    c.movetofirst();               //final string username = c.getstring(0);               //final string password = c.getstring(1);               //string cloud = "avarote1994@gmail.com";               // authenticate , instance of session server               session = session.getinstance(properties,new javax.mail.authenticator() {                   protected passwordauthentication getpasswordauthentication() {                       return new passwordauthentication(username, password);                   }               });               try {                   store = session.getstore("imaps");                       store.connect();                   inbox = store.getfolder("inbox");         // access inbox of recepient's email id                   inbox.open(folder.read_write);                // open inbox in read-write mode                   message messages[] = inbox.search(new flagterm(new flags(flag.seen), false));     //search inbox unread mails                   system.out.println("number of mails = " + messages.length);                   (int = 0; < messages.length; i++) {               // process unread mails                         message = messages[i];                         address[] = message.getfrom();                         string from1 = from[0].tostring();                         system.out.println(from1);                         if(from1.contains("<")){                             int start = from1.indexof("<");                             int end = from1.indexof(">");                             fromsubstring = from1.substring(start+1,end);   // retrieve sender's email id                         } else{                             fromsubstring = from1;                         }                         system.out.println(fromsubstring);                         //if(fromsubstring.equals(cloud)){      // check whether mail cloud                         string[] subject = message.getsubject().split(","); // split subject                         system.out.println("hi");                         type = subject[0];                                  // store details in respective variables                         phonenumber =subject[1];                         name = subject[2];                         system.out.println(type);                         system.out.println(phonenumber);                         system.out.println(name);                         //string body=message.getcontenttype().tostring();                        // system.out.print(body);                          processmessagebody(message);                          //system.out.println("--------------------------------");                 //        }                     }                     inbox.close(true);                     store.close();                 }                 catch (nosuchproviderexception e) {                     e.printstacktrace();                 }                 catch (messagingexception e) {                     e.printstacktrace();                 }               }            }        public void processmessagebody(message message) {           try {                object content = message.getcontent();               string msg=content.tostring();                system.out.println(msg);               if (content instanceof multipart) {                       // if mail has multipart message                   multipart multipart = (multipart) content;                   procesmultipart(multipart);               }               else{                   system.out.println("content = "+content);                   processsinglepart(content.tostring());               }            }           catch (ioexception e) {               e.printstacktrace();           }           catch (messagingexception e) {               e.printstacktrace();           }       }        public void processsinglepart(string content){         string[] body = content.split(",");         // split contents of body         system.out.println('1');            time_stamp = body[0];                             // store details in respective variables           command = body[3];           system.out.println(time_stamp);           //tts.speak(time_stamp, texttospeech.queue_flush, null);           publishprogress(command);         }      public void procesmultipart(multipart content) {           system.out.println("amulya");           try {               bodypart bodypart = content.getbodypart(0);               // retrieve contents body               object o;                o = bodypart.getcontent();               if (o instanceof string) {               system.out.println("content multipart= "+o.tostring());               processsinglepart(o.tostring());               }           }            catch (ioexception e) {               e.printstacktrace();           }            catch (messagingexception e) {               e.printstacktrace();           }        }     }      @override     protected void oncreate(bundle savedinstancestate) {         c = this.getapplicationcontext();          super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);           readmailsample rd=new readmailsample(getapplicationcontext());     system.out.println("hello");     rd.execute();     system.out.println("------xvsdfsdfsd---------aefaefa-----------------");     }         @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();         if (id == r.id.action_settings) {             return true;         }         return super.onoptionsitemselected(item);     } } 

i trying implement texttospeech functionality. getting above error. functionality runs in async thread. async thread reads mail. want convert read mail speech.

logcat: 02-08 21:45:26.009: i/system.out(12947): testing 02-08 21:45:26.709: i/system.out(12947): javax.mail.internet.mimemultipart@21cdab10 02-08 21:45:26.709: i/system.out(12947): amulya 02-08 21:45:27.279: i/system.out(12947): content multipart= 1221235487821,ad:af:12:2a:d5:c8,high,it thursday take tablets,future  02-08 21:45:27.279: i/system.out(12947): use,future use,future use  02-08 21:45:27.279: i/system.out(12947): 1 02-08 21:45:27.279: i/system.out(12947): 1221235487821 02-08 21:45:27.279: i/system.out(12947): ---------------------adasd-----------1221235487821 02-08 21:45:27.299: w/texttospeech(12947): speak failed: tts engine connection not set 02-08 21:45:29.689: d/dalvikvm(12947): gc_for_alloc freed 455k, 19% free 5727k/7068k, paused 7ms, total 7ms 02-08 21:45:30.499: i/system.out(12947): number of mails = 0 02-08 21:45:36.029: i/system.out(12947): number of mails = 0 

from api docs see implementation of oninitlistener necessesary receive notification tts initialization complete. described in texttospeech docs stated speech fail until initialized. in following, not attempt fix of code works, point out how you're mis-using tts framework , nudge in right direction.

though have implemented oninitlistener, aren't doing useful implementation. below, show more useful in 1 captures result of oninitlistener's oninit callback, in order remain aware of initialization state of tts.

... private boolean misttsinited = false; // member indicating tts initialization state ... // receives notification of initialization public void oninit(int status) {     if (status == texttospeech.success) {         log.i("tts", "initilization succeeded!");         // save state indicating success of tts initialization         misttsinited = true;         ...     } else {         log.e("tts", "initilization failed!");         misttsinited = false;     } } ... ... @override protected void onprogressupdate(string... values) {     // trivial example show how can check state of initialization tts     if (misttsinited) {         shownotification();          speakout();     } else {         // code deal tts not init'ed case     } } 

additionally, wouldn't run texttospeech initialization part of asynctask. if you, i'd move

tts = new texttospeech(c,readmailsample.this);

into oncreate rather onpreexecute. see used in tutorials seen here, , here


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -