Skip to content

Cannot make it to work with tts #55

Description

@diegopefm

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions