I don't understand how to play text to speech with audio focus (there is spotify running some music when I try my app),
I don't understand the audioFocusChangeListener.
Here is my code:
public class MainActivity extends AppCompatActivity {
AudioManager.OnAudioFocusChangeListener audioFocusChangeListener;
public void playTTSWithAudioFocus(String toSpeak) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(audioFocusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request temporary focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null,null);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playTTSWithAudioFocus("hello world");
}
}
Please help me.