2014-04-13 64 views
0

我的代碼如下無法運行Android文本到語音

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

    runBtn = (Button)findViewById(R.id.button_run_demo); 
    ttobj = new TextToSpeech(getApplicationContext(), 
      new TextToSpeech.OnInitListener() { 
      @Override 
      public void onInit(int status) { 
       if (status != TextToSpeech.ERROR) { 
        ttobj.setLanguage(Locale.UK); 
       } 
      } 
     }); 
    runBtn.setOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

     } 
    }); 
} 

public void onPause() { 
    if (ttobj != null) { 
     ttobj.stop(); 
     ttobj.shutdown(); 
    } 
    super.onPause(); 
} 

public void speakText(String text) { 
    String toSpeak = text; 
    Toast.makeText(getApplicationContext(), toSpeak, 
     Toast.LENGTH_SHORT).show(); 
    ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_PIC_REQUEST) { 

     //do my stuff 

     speakText("Hello"); 

    } 
} 

雖然我能夠做我的東西,我不能夠運行speech

我錯過了什麼?

感謝所有

回答

1

onActivityResult()被稱爲TextToSpeech已經關機,因爲你關閉它onPause()。當TextToSpeech正在重新初始化時,您必須設置一個標誌以說出「您好」,或者將您的代碼置於onPause()onDestroy()中。