All Downloads are FREE. Search and download functionalities are using the official Maven repository.

browserMain.nl.marc_apps.tts.TextToSpeechFactory.kt Maven / Gradle / Ivy

Go to download

Kotlin Multiplatform Text-to-Speech library for Android and browser (Kotlin/JS & Kotlin/Wasm). This library will enable you to use Text-to-Speech in multiplatform Kotlin projects.

There is a newer version: 3.0.0-beta.1
Show newest version
package nl.marc_apps.tts

import js_interop.Window
import js_interop.isSpeechSynthesisSupported
import js_interop.window
import nl.marc_apps.tts.errors.TextToSpeechNotSupportedError

/**
 * Factory class to create a Text-to-Speech instance.
 */
actual class TextToSpeechFactory(
    private val context: Window = window
) {
    actual val isSupported = isSpeechSynthesisSupported

    actual val canChangeVolume = true

    actual suspend fun create(): Result {
        return if (isSupported) {
            Result.success(TextToSpeechBrowser(context))
        } else {
            Result.failure(TextToSpeechNotSupportedError())
        }
    }

    @Throws(RuntimeException::class)
    actual suspend fun createOrThrow(): TextToSpeechInstance {
        return create().getOrThrow()
    }

    actual suspend fun createOrNull(): TextToSpeechInstance? {
        return create().getOrNull()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy