mercredi 10 mai 2017

How do I pass a random string variable through a textview so that it can then be spoken by the TextToSpeech method?

I have played around with several different options that I have come across when researching this issue but none of them seem to cover the scope of what I'm trying to accomplish. At the moment I can set the TextView tv1 to an EditText view which allows the user input on EditText to pop up in a toast and the TextToSpeech engine speaks the value of EditText. I would rather an invisible TextView that randomly assigns a string from translationLibrary so that the TextToSpeech engine speaks the random string as well as displays the string in a toast.

public class MainActivity extends AppCompatActivity {
Button shareBtn;
Intent shareIntent;
String shareBody = "Find out what your grandpa's butt is saying with this classy app. <INSERT STORE LINK>";
TextToSpeech tts1;
TextView tv1;
Button b1;`

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.textView);
    b1 = (Button) findViewById(R.id.recordButton);`


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


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

    });

    ArrayList<String> translationLibrary = new ArrayList<String>();
    translationLibrary.add("Donde esta la bibliotecha");
    translationLibrary.add("Hello");
    translationLibrary.add("Goodbye");
    translationLibrary.add("Some like it hot");


    shareBtn = (Button) findViewById(R.id.share);

    shareBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            shareIntent = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My App");
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(shareIntent, "Share via"));

        }


        });
}

}




Aucun commentaire:

Enregistrer un commentaire