Reputation: 1048
Hi guys
I've been looking around but cannot seem to find a suitable answer to integrate into my function. I am basically using the following code currently:
private void sayHello(String timeString) {
textToSpeech.speak(timeString,
TextToSpeech.QUEUE_FLUSH,
null);
}
This code works fine but it's too loud and it can only be controlled by the volume of the device itself. I want to be able to adjust/hardcode/be able to use spinner to control the volume of the TTS but cannot seem to do so accordingly.
Is this functionality available for this library? Is it achievable?
I've also tried to implement the following into my code:
KEY_PARAM_VOLUME
However, I cannot see any examples of this being used and it's showing up with the error to create a function. Any advice?
Upvotes: 2
Views: 2095
Reputation: 1
Years ago I asked Anki (Damien) for the ability to set volume of TTS. He put it on a list of things to consider but nothing has been done since. Is it that difficult a problem? And I can't believe more people aren't asking for volume control. I have a downloaded deck of Italian cards with audio and my cards play that audio followed by 2 TTS voices to hear three different pronunciations. Unfortunately the TTS is much louder so I either hurt my ears or I can't hear the downloaded audio. The answer provided by Trusty Turtle looks good but I use a Mac and an iPad. I don't know enough programming to translate the code myself. Ideally there should be a generalized version handling any OS. Looks like I have to be patient a few more years.
Upvotes: 0
Reputation: 7437
public class MainActivity extends AppCompatActivity {
int androidAPILevel = android.os.Build.VERSION.SDK_INT;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
start();
}
});
}
private void start() {
if (androidAPILevel < 21) {
HashMap<String,String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, "0.5"); // change the 0.5 to any value from 0-1 (1 is default)
tts.speak("This is a volume test.", TextToSpeech.QUEUE_FLUSH, params);
} else { // android API level is 21 or higher...
Bundle params = new Bundle();
params.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, 0.5f); // change the 0.5f to any value from 0f-1f (1f is default)
tts.speak("This is a volume test.", TextToSpeech.QUEUE_FLUSH, params, null);
}
}
}
Upvotes: 5