Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import android.os.AsyncTask;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8.  
  9. import com.ibm.watson.developer_cloud.android.library.audio.StreamPlayer;
  10. import com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech;
  11. import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15. private TextView textView;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21.  
  22. final EditText editText = (EditText) findViewById(R.id.editText);
  23. Button button = (Button) findViewById(R.id.button);
  24. textView = (TextView) findViewById(R.id.textView);
  25.  
  26. button.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. String textToSpeak = editText.getText().toString();
  30. System.out.println("the text to speech: " + textToSpeak);
  31. textView.setText("TTS: " + textToSpeak);
  32.  
  33. new WatsonTask().execute(textToSpeak);
  34. }
  35. });
  36. }
  37.  
  38. private TextToSpeech initTextToSpeechService() {
  39. TextToSpeech service = new TextToSpeech();
  40. String username = "<your username>";
  41. String password = "<your password>";
  42. service.setUsernameAndPassword(username, password);
  43. return service;
  44. }
  45.  
  46. private class WatsonTask extends AsyncTask<String, Void, String> {
  47.  
  48. @Override
  49. protected String doInBackground(String... textToSpeak) {
  50. runOnUiThread(new Runnable() {
  51. @Override
  52. public void run() {
  53. textView.setText("running the Watson thread");
  54. }
  55. });
  56.  
  57. TextToSpeech textToSpeech = initTextToSpeechService();
  58. new StreamPlayer().playStream(textToSpeech.synthesize(String.valueOf(textToSpeak[0]), Voice.EN_MICHAEL).execute());
  59.  
  60. return "text to speech done";
  61. }
  62.  
  63. @Override
  64. protected void onPostExecute(String result) {
  65. textView.setText("TTS status: " + result);
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement