2015-02-05 115 views
0

TextTo Speech no works.It顯示服務沒有啓動,我已經從谷歌play.it安裝文本到語音,它是正常工作時,它是附加按鈕,但停止工作時,我把它移動onCreate ()。它結合successfulu與TTS引擎和顯示文本長度TextToSpeech服務沒有啓動

public class MainActivity extends Activity implements SensorEventListener{ 
    private ImageView image; 

    // record the compass picture angle turned 
    private float currentDegree = 0f; 

    // device sensor manager 
    private SensorManager mSensorManager; 

    TextView tvHeading; 

    Campus_guide_Databasehelper cgh; 
    float []array=new float[50]; 
    Button btnShowLocation; 
    Button addLocation; 
    Button btnshow; 
    Button btnNearest_point; 
    EditText lat; 
    EditText longi; 
    EditText texte; 
    TextToSpeech ttsobj; 
    float bearing; 
    // GPSTracker class 
    GPSTracker gps; 
    Location location; 
    Location curr_loc; 
    Location dest_loc; 
    double latitude; 
    double longitude; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.compass); 
     cgh=new Campus_guide_Databasehelper(this); 

     gps = new GPSTracker(MainActivity.this); 
     image = (ImageView) findViewById(R.id.imageViewCompass); 

     // TextView that will tell the user what degree is he heading 
     tvHeading = (TextView) findViewById(R.id.tvHeading); 

     // initialize your android device sensor capabilities 
     mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 

     ttsobj=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener() { 

     @Override 
     public void onInit(int status) { 
      // TODO Auto-generated method stub 
      if(status==TextToSpeech.SUCCESS) 
      { 
       int result=ttsobj.setLanguage(Locale.US); 
       if(result==TextToSpeech.LANG_MISSING_DATA || result==TextToSpeech.LANG_NOT_SUPPORTED) 
       { 
        Log.e("TTS","This Language is Not supported"); 
        Intent installation=new Intent(); 
        installation.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
        startActivity(installation); 
       } 
       else{ 
        Log.e("TTS","Installation Failed"); 
       } 
      } 

     } 
     }); 

     ttsobj.speak("speak some thing",TextToSpeech.QUEUE_FLUSH, null); 
     // check if GPS enabled  
     if(gps.canGetLocation()){ 

     latitude = gps.getLatitude(); 
     longitude = gps.getLongitude(); 
     latitude=Math.round(latitude*1000000.0)/1000000.0; 
     longitude=Math.round(longitude*1000000.0)/1000000.0; 
     curr_loc=new Location(" "); 
     dest_loc=new Location(" "); 
     curr_loc.setLatitude(latitude); 
     curr_loc.setLongitude(longitude); 
     String data=cgh.Nearest_point(latitude, longitude); 
     String[] split=data.split(":"); 
     for(int i=1;i<split.length;i++) 
     { 
      String [] fur_split=split[i].split(" "); 
      Double llti=Double.parseDouble(fur_split[0]); 
      Double llong=Double.parseDouble(fur_split[1]); 
      dest_loc.setLatitude(llti); 
      dest_loc.setLongitude(llong); 
      bearing=curr_loc.bearingTo(dest_loc); 
      Toast.makeText(this,split[i-1], Toast.LENGTH_LONG).show(); 
      Toast.makeText(this,String.valueOf(bearing), Toast.LENGTH_LONG).show(); 


      ttsobj.speak("text to speech works",TextToSpeech.QUEUE_FLUSH,null); 

      i++; 
     } 
     // \n is for new line 


     Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();  
     }else{ 
     // can't get location 
     // GPS or Network is not enabled 
     // Ask user to enable GPS/network in settings 
     gps.showSettingsAlert(); 
     } 

回答

0

首先你else是在錯誤的地方,應該是與之相匹配的第一if

ttsobj=new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener() { 

    @Override 
    public void onInit(int status) { 
     // TODO Auto-generated method stub 
     if(status==TextToSpeech.SUCCESS) 
     { 
      int result=ttsobj.setLanguage(Locale.US); 
      if(result==TextToSpeech.LANG_MISSING_DATA || result==TextToSpeech.LANG_NOT_SUPPORTED) 
      { 
       Log.e("TTS","This Language is Not supported"); 
       Intent installation=new Intent(); 
       installation.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
       startActivity(installation); 
      } 

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

其次,你不能叫speak聯合國直到onInit被調用。

+0

onInit called.logcat顯示消息oninit成功調用它也顯示文本到語音的長度,但之後它顯示服務未啓動 – 2015-02-06 07:20:39

+0

什麼服務?發佈你的logcat。 – 2015-02-06 22:12:05