2017-05-06 154 views

回答

-1

您應該必須使用谷歌功能文本到語音。

public class MainActivity extends AppCompatActivity { 
    TextToSpeech t1; 
    EditText ed1; 
    Button b1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ed1=(EditText)findViewById(R.id.editText); 
     b1=(Button)findViewById(R.id.button); 

     t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      if(status != TextToSpeech.ERROR) { 
       t1.setLanguage(Locale.UK); 
      } 
     } 
     }); 

     b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String toSpeak = ed1.getText().toString(); 
      Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show(); 
      t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); 
     } 
     }); 
    } 

    public void onPause(){ 
     if(t1 !=null){ 
     t1.stop(); 
     t1.shutdown(); 
     } 
     super.onPause(); 
    } 
} 
相關問題