
Simplifies speech-to-text integration with a unified API across platforms. Offers reactive API, Jetpack Compose compatibility, error handling, and minimal setup for seamless application integration.
SpeechToTextKit is a Kotlin Multiplatform library that provides a simple and unified API for speech-to-text functionality across multiple platforms: Android, iOS, Desktop (JVM), and Web (Wasm).
rememberSpeechToText()
The library is available on Maven Central via Sonatype.
Add the following to your module's build.gradle.kts:
dependencies {
// Core library
implementation("io.github.eslamwael74.speechtotextkit:speechToText:1.0.0")
// Optional: Compose UI components
implementation("io.github.eslamwael74.speechtotextcompose:speechToTextCompose:1.0.0")
}There are currently two ways to use this library:
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import io.github.eslamwael74.speechtotextcompose.rememberSpeechToText
@Composable
fun SpeechRecognitionScreen() {
var recognizedText by remember { mutableStateOf("") }
val speechRecognizer = rememberSpeechToText()
var isListening by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
speechRecognizer.results.collect { result ->
recognizedText = result.text
}
}
Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
Text(
text = recognizedText.ifEmpty { "Tap the button and speak" },
modifier = Modifier.weight(1f)
)
Button(onClick = {
if (isListening) {
// Stop listening
speechRecognizer.stopListening()
isListening = false
} else {
// Start listening
speechRecognizer.startListening()
isListening = true
}
}) {
Text(if (isListening) "Stop Listening" else "Start Listening")
}
}
}import com.eslamwael74.speechtotext.SpeechRecognizer
import com.eslamwael74.speechtotext.SpeechRecognizerFactory
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
// Using dependency injection
class YourViewModel(
private val speechRecognizer: SpeechRecognizer
) {
init {
// Listen for speech recognition results
speechRecognizer.results.onEach { result ->
// Handle result
println("Recognized text: ${result.text}")
}.launchIn(viewModelScope)
// Monitor state changes
speechRecognizer.state.onEach { state ->
// Handle state changes
println("Recognition state: $state")
}.launchIn(viewModelScope)
}
fun startListening() {
viewModelScope.launch {
speechRecognizer.startListening()
}
}
fun stopListening() {
viewModelScope.launch {
speechRecognizer.stopListening()
}
}
fun cleanup() {
speechRecognizer.destroy()
}
}
// Example of factory/provider to create the SpeechRecognizer
class SpeechRecognizerProvider(
private val applicationContext: Context
) {
fun provideSpeechRecognizer(): SpeechRecognizer {
return SpeechRecognizerFactory(applicationContext).createSpeechRecognizer()
}
}
// Usage with Manual DI
class YourActivity : AppCompatActivity() {
private val speechRecognizerProvider by lazy {
SpeechRecognizerProvider(applicationContext)
}
private val viewModel by viewModels {
viewModelFactory {
YourViewModel(speechRecognizerProvider.provideSpeechRecognizer())
}
}
}
// Or with Hilt/Dagger
@Module
@InstallIn(SingletonComponent::class)
object SpeechModule {
@Provides
@Singleton
fun provideSpeechRecognizer(@ApplicationContext context: Context): SpeechRecognizer {
return SpeechRecognizerFactory(context).createSpeechRecognizer()
}
}Add the following permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />You'll also need to request this permission at runtime.
Add the following to your Info.plist:
<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to your microphone for speech recognition</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>This app uses speech recognition to convert your speech to text</string>The following features are planned but not yet implemented:
Check out the included example app in the /example directory for a complete implementation of SpeechToTextKit.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Distributed under the Apache 2.0 License. See LICENSE for more information.
Eslam Wael - @eslamwael74
Project Link: https://github.com/eslamwael74/speechtotextkit
SpeechToTextKit is a Kotlin Multiplatform library that provides a simple and unified API for speech-to-text functionality across multiple platforms: Android, iOS, Desktop (JVM), and Web (Wasm).
rememberSpeechToText()
The library is available on Maven Central via Sonatype.
Add the following to your module's build.gradle.kts:
dependencies {
// Core library
implementation("io.github.eslamwael74.speechtotextkit:speechToText:1.0.0")
// Optional: Compose UI components
implementation("io.github.eslamwael74.speechtotextcompose:speechToTextCompose:1.0.0")
}There are currently two ways to use this library:
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import io.github.eslamwael74.speechtotextcompose.rememberSpeechToText
@Composable
fun SpeechRecognitionScreen() {
var recognizedText by remember { mutableStateOf("") }
val speechRecognizer = rememberSpeechToText()
var isListening by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
speechRecognizer.results.collect { result ->
recognizedText = result.text
}
}
Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
Text(
text = recognizedText.ifEmpty { "Tap the button and speak" },
modifier = Modifier.weight(1f)
)
Button(onClick = {
if (isListening) {
// Stop listening
speechRecognizer.stopListening()
isListening = false
} else {
// Start listening
speechRecognizer.startListening()
isListening = true
}
}) {
Text(if (isListening) "Stop Listening" else "Start Listening")
}
}
}import com.eslamwael74.speechtotext.SpeechRecognizer
import com.eslamwael74.speechtotext.SpeechRecognizerFactory
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
// Using dependency injection
class YourViewModel(
private val speechRecognizer: SpeechRecognizer
) {
init {
// Listen for speech recognition results
speechRecognizer.results.onEach { result ->
// Handle result
println("Recognized text: ${result.text}")
}.launchIn(viewModelScope)
// Monitor state changes
speechRecognizer.state.onEach { state ->
// Handle state changes
println("Recognition state: $state")
}.launchIn(viewModelScope)
}
fun startListening() {
viewModelScope.launch {
speechRecognizer.startListening()
}
}
fun stopListening() {
viewModelScope.launch {
speechRecognizer.stopListening()
}
}
fun cleanup() {
speechRecognizer.destroy()
}
}
// Example of factory/provider to create the SpeechRecognizer
class SpeechRecognizerProvider(
private val applicationContext: Context
) {
fun provideSpeechRecognizer(): SpeechRecognizer {
return SpeechRecognizerFactory(applicationContext).createSpeechRecognizer()
}
}
// Usage with Manual DI
class YourActivity : AppCompatActivity() {
private val speechRecognizerProvider by lazy {
SpeechRecognizerProvider(applicationContext)
}
private val viewModel by viewModels {
viewModelFactory {
YourViewModel(speechRecognizerProvider.provideSpeechRecognizer())
}
}
}
// Or with Hilt/Dagger
@Module
@InstallIn(SingletonComponent::class)
object SpeechModule {
@Provides
@Singleton
fun provideSpeechRecognizer(@ApplicationContext context: Context): SpeechRecognizer {
return SpeechRecognizerFactory(context).createSpeechRecognizer()
}
}Add the following permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />You'll also need to request this permission at runtime.
Add the following to your Info.plist:
<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to your microphone for speech recognition</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>This app uses speech recognition to convert your speech to text</string>The following features are planned but not yet implemented:
Check out the included example app in the /example directory for a complete implementation of SpeechToTextKit.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Distributed under the Apache 2.0 License. See LICENSE for more information.
Eslam Wael - @eslamwael74
Project Link: https://github.com/eslamwael74/speechtotextkit