android-audio-visualizer icon indicating copy to clipboard operation
android-audio-visualizer copied to clipboard

Cannot make it to work with tts

Open diegopefm opened this issue 2 years ago • 0 comments

Is it possible to use the visualizer with Android text-to-speech?

My app uses Android text-to-speech to read paragraphs, and I'd like to add an audio visualizer so to have an animation according to the speech.

I find this audio visualizer very interesting, but I don't know how to use it with tts.

In the page examples it says you have to instantiate Media player, and shows this example:

mediaPlayer = MediaPlayer.create(this, R.raw.you_music);

But the fact is I'm not using MediaPlayer, but TTS for the audio. The library requires an audiosessionid to be passed to the player this way:

lineVisualizer.setPlayer(mediaPlayer.getAudioSessionId());

But I'm not finding any way to get the audiosessionid from tts, and inspected all the properties of the TextToSpeech instance, but no getAudioSessionId() or similar is present.

private lateinit var ttobj: TextToSpeech
    
    ttobj = TextToSpeech(this, this)
    ttobj.setLanguage(Locale.US)
    
    ttobj.setOnUtteranceProgressListener(object : UtteranceProgressListener() {
        override fun onStart(utteranceId: String) {
            //Here I would call setPlayer and pass an audiosessonid to start the visualizer.
        }
    
        override fun onDone(utteranceId: String) {
            runOnUiThread {
                ttsSpeechFinished()
            }
        }
    
        override fun onError(utteranceId: String) {
            Log.i("TextToSpeech", "On Error")
        }
    })
    
    fun doSpeech(){
        if (ttsStatus == TextToSpeech.SUCCESS){
            ttobj.speak("I'm going to read a paragraph for testing, right?", TextToSpeech.QUEUE_FLUSH, null,"")
        }
    }

The speech is working great, but I don't know how to "attach" the audio visualizer to the current tts audio sinthesys.

I've tried using AudioManager, that has a generateAudioSessionId() method:

var mAudioManager = activity?.getSystemService(AUDIO_SERVICE) as AudioManager
    val audioSessionId = mAudioManager.generateAudioSessionId()

But this audioSessionId is not working, and I'm obviously not sure I'm in the right direction.

Any help?

diegopefm avatar Jun 24 '23 22:06 diegopefm