Chase Roberts
Chase Roberts

Reputation: 9376

How to change the volume of Text To Speech when not speaking?

I have a setting in my app that reads scores to the user. The problem is that reading the score is pretty quick. i.e. "7", "8", "10". And when the user tries to adjust the volume using the hardware buttons in changes the ringer volume instead of the media volume. Is there any way to handle this?

The code I am using:

TextToSpeech tts = new TextToSpeech(applicationContext, null);

later, after tts successfully initializes...

int score = 7;
tts.speak(Integer.toString(score), TextToSpeech.QUEUE_ADD, null);

Upvotes: 0

Views: 1036

Answers (1)

dthulke
dthulke

Reputation: 949

You can achieve this by calling setVolumeControlStream(AudioManager.STREAM_MUSIC); in your activity.

From the documentation on setVolumeControlStream:

Suggests an audio stream whose volume should be changed by the hardware volume controls.

The suggested audio stream will be tied to the window of this Activity. Volume requests which are received while the Activity is in the foreground will affect this stream.

Upvotes: 1

Related Questions