Guest User

Untitled

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