battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.


battle programming a forum for elite programmers with extreme will power to sharpen their skills
 
HomeGallerySearchRegisterLog in
 

 Android Studio alchemy series : TTS Test To Speech

Go down 
AuthorMessage
Moti Barski
super
super
Moti Barski

Posts : 568
Join date : 2011-08-02

Android Studio alchemy series : TTS Test To Speech Empty
PostSubject: Android Studio alchemy series : TTS Test To Speech   Android Studio alchemy series : TTS Test To Speech EmptyThu Feb 25, 2021 2:17 am

Code:
package com.yotamarker.lg200221

import android.speech.tts.TextToSpeech
import android.util.Log
import java.util.*

class TTSV2(tts: TextToSpeech?) : TextToSpeech.OnInitListener{
    //see Mouth class for documentation
    private val tts:TextToSpeech? = tts
    override fun onInit(status:Int) {
        if (status == TextToSpeech.SUCCESS&&tts!=null) {
            // set US English as language for tts
            val result = tts!!.setLanguage(Locale.US)

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS","The Language specified is not supported!")
            } else {
                //buttonSpeak!!.isEnabled = true
            }

        } else {
            Log.e("TTS", "Initilization Failed!")
        }
    }
}

2nd class you need to add :

Code:
package com.yotamarker.lg200221

import android.speech.tts.TextToSpeech
class Mouth(tts:TextToSpeech) {
    /* setup :
    1 add this class and TTSV2.kt class to your project
    2 add this 3 code lines to MianActivity :
    private var tts: TextToSpeech? = null;private var mouth: Mouth? = null //tts setup line 1 of 3 global var declaration
    tts = TextToSpeech(this, TTSV2(tts));mouth= Mouth(tts!!)//tts setup line 2 of 3 place in onCreate methode
    mouth!!.onDestroy()//tts setup line 3 of 3 place in onDestroy() of MainActivity
    3 use example : mouth!!.speakOut("hadouken")
    * */
    private val tts:TextToSpeech
    init{
        this.tts = tts
    }
    fun speakOut(sentence:String) {
        tts.speak(sentence, TextToSpeech.QUEUE_FLUSH, null, "")
    }
    fun onDestroy() {
        if (tts != null) {
            tts!!.stop()
            tts!!.shutdown()
        }
    }
}

see class doc for how to use in the main activity chobit

_________________
MB over and out emp it up
Back to top Go down
 
Android Studio alchemy series : TTS Test To Speech
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: the LivinGrimoire-
Jump to: