MiTextToSpeech is the simplest way to integrate TextToSpeech functionality into your application.
video.mp4
- Android 15 support.
- Simple implementation.
- highlighting current sentence.
- auto scroll text while text-to-speech play.
- Set your custom styles for text highlighting.
- Set your own language, pitch & speech Rate.
- Inbuilt Functionality Support play, pause, backward and forward[one sentence], slider position, etc.
-
Step 1. Add the JitPack repository to your project build.gradle:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
or
If Android studio version is Arctic Fox then add it in your settings.gradle:
dependencyResolutionManagement { repositories { ... maven { url 'https://jitpack.io' } } }
-
Step 2. Add the dependency in your app module build.gradle:
dependencies { ... implementation 'com.github.Mindinventory:MiTextToSpeech:X.X.X' }
-
Step 1. Initialization of the MiTextToSpeech inside your viewmodel :
... lateinit var instanceOfTTS: MiTextToSpeechEngine init { initTTS(context) } private fun initTTS(context: Context): MiTextToSpeechEngine { instanceOfTTS = MiTextToSpeechEngine .getInstance() .init(context) .setLanguage(Locale.ENGLISH) .setPitchAndSpeed(1f, 1f) .setText(context.getString(R.string.text_to_speech_text)) return instanceOfTTS } ...
-
Step 2. Add listeners and MITextToSpeechText inside your composable :
... var instanceOfTTS by remember { mutableStateOf<MiTextToSpeechEngine?>(null) } LaunchedEffect(instanceOfTTS == null) { instanceOfTTS = viewModel.instanceOfTTS } ... // Listeners to get status tts.setOnCompletionListener { Log.e("TAG", "TTSScreen: Completed From Callback") }.setOnErrorListener { //Perform action for error }.setOnEachSentenceStartListener { Log.e("TAG", "TTSScreen: onEachSentenceStart is called") } ... // The composable function displays the text and helps us to highlight the currently spoken sentence. TTSComposable( tts = tts, textAlign = TextAlign.Center, fontFamily = fontFamily, fontWeight = FontWeight.ExtraLight, miTextHighlightBuilder = MITextHighlightBuilder( text = tts.mainText, tts.highlightTextPair.value, style = SpanStyle( fontFamily = fontFamily, color = Amaranth, fontWeight = FontWeight.Bold, ) ), style = TextStyle( fontSize = 20.sp, color = Color.Black, lineHeight = 35.sp ), ) ...
Functions | Description |
---|---|
playTextToSpeech() | used for play TextToSpeech content |
pauseTextToSpeech() | pause the Text-to-speech if it is currently speaking |
forwardText() | Moves to the next sentence |
backwardText() | Moves to the preview sentence |
setPitchAndSpeed() | you can customize the pitch and speech as per your requirements. (float, float) |
Contribution towards our repository is always welcome, we request contributors to create a pull request to the develop branch only.
It would be great for us if the reporter can share the below things to understand the root cause of the issue.
- Library version
- Code snippet
- Logs if applicable
- Device specification like (Manufacturer, OS version, etc)
- Screenshot/video with steps to reproduce the issue
- minSdkVersion >= 24
- Androidx
MiTextToSpeech is MIT-licensed.
We’d be really happy if you send us links to your projects where you use our component. Just send an email to [email protected] And do let us know if you have any questions or suggestion regarding our work.