一个简单有效的库,为Android应用程序提供文本到语音功能。
使用Gradle将库添加到您的项目中:
allprojects {
repositories {
maven { url ' https://jitpack.io ' }
}
}dependencies {
implementation ' com.github.aiyu-ayaan:tts-engine:Tag '
}使用库简单而简单。这是如何在代码中使用它的示例:
TextToSpeechHelper
.getInstance(activity)
.registerLifecycle(owner)
.speak(message)
.highlight()
.onHighlight { pair ->
Log .d( TAG , " speak: ${pair.first} - ${pair.second} " )
}
.onDone {
Log .d( TAG , " speak: done " )
}
.onError {
Log .d( TAG , " speak: $it " )
} activity是将用于初始化TTS发动机的活动。
owner是生命周期所有者,将用于注册TTS发动机生命周期观察者并可以处理生命周期事件。
message是将要说的文字。
highlight()是一种可选方法,它将突出文本中的口语。
onHighlight()是一种可选方法,当说单词时将被调用。
onDone()是一种说文本时将调用的方法。
onError()是发生错误时将调用的方法。
MainActivity.kt
class MainActivity : ComponentActivity () {
override fun onCreate ( savedInstanceState : Bundle ? ) {
super .onCreate(savedInstanceState)
setContent {
TTSLibraryTheme {
val viewModel = MainViewModel ()
val resource = stringResource(id = R .string.des)
val text = remember { resource }
// screen
Surface (
modifier = Modifier .fillMaxSize(),
color = MaterialTheme .colorScheme.background
) {
Screen (
state = text,
viewModel = viewModel,
) {
speak(
this ,
this as LifecycleOwner ,
text,
viewModel
)
}
}
}
}
}
}
@Composable
fun Screen (
modifier : Modifier = Modifier ,
viewModel : MainViewModel ,
state : String ,
onButtonClick : () -> Unit = {},
) {
Column (
modifier = modifier
.fillMaxSize()
.padding( 16 .dp),
verticalArrangement = Arrangement . Center ,
horizontalAlignment = Alignment . CenterHorizontally
) {
val s = viewModel.getState().value
TTSText (
textAlign = TextAlign . Center ,
textHighlightBuilder = TextHighlightBuilder (
text = state,
s
),
)
Spacer (modifier = Modifier .padding( 16 .dp))
Button (onClick = onButtonClick) {
Text (text = stringResource(id = R .string.speak))
}
}
}
private fun speak (
activity : Activity ,
owner : LifecycleOwner ,
message : String ,
viewModel : MainViewModel
) {
TextToSpeechHelper
.getInstance(activity)
.registerLifecycle(owner)
.speak(message)
.highlight()
.onHighlight { pair ->
viewModel.updateState(pair)
}
.onDone {
Log .d( TAG , " speak: done " )
}
.onError {
Log .d( TAG , " speak: $it " )
}
}
MainViewModel.kt
class MainViewModel : ViewModel () {
private val state = mutableStateOf( Pair ( 0 , 0 ))
fun updateState ( pair : Pair < Int , Int >) {
state.value = pair
}
fun getState (): State < Pair < Int , Int >> = state
} class MainActivity : AppCompatActivity () {
override fun onCreate ( savedInstanceState : Bundle ? ) {
super .onCreate(savedInstanceState)
setContentView( R .layout.activity_main)
val textView = findViewById< TextView >( R .id.text_view)
val button = findViewById< Button >( R .id.button)
val message = getString( R .string.des)
button.setOnClickListener {
speak(
this ,
this as LifecycleOwner ,
message,
textView
)
}
}
private fun speak (
activity : Activity ,
owner : LifecycleOwner ,
message : String ,
textView : TextView
) {
TextToSpeechHelper
.getInstance(activity)
.registerLifecycle(owner)
.speak(message)
.highlight()
.onHighlight { pair ->
textView.highlightText(pair.first, pair.second)
}
.onDone {
Log .d( TAG , " speak: done " )
}
.onError {
Log .d( TAG , " speak: $it " )
}
}
}TextView.highlightText()是一种扩展方法,它突出了文本中的口语。 Method 1: registerLifecycle(owner: LifecycleOwner)记录给定所有者的生命周期。
Method 2: initTTS()初始化文本到语音引擎。
Method 3: speak(message: String)从给定消息生成语音。
Method 4: highlight()突出显示文本视图中的文本。
Method 5: removeHighlight()从文本视图中的文本中删除了亮点。
Method 6: destroy(action: (() -> Unit))破坏文本对语音引擎并删除所有听众。
Method 7: onStart(onStartListener: () -> Unit)设置onstart侦听器。
Method 8: onDone(onCompleteListener: () -> Unit)设置了ondone侦听器。
Method 9: onError(onErrorListener: (String) -> Unit)设置了OnError侦听器。
Method 10: onHighlight(onHighlightListener: (Pair<Int, Int>) -> Unit)设置了on -ighlight侦听器。
Method 11: setCustomActionForDestroy(action: () -> Unit)设置了在破坏文本到语音发动机时要执行的自定义操作。
Method 12: setLanguage(locale: Locale)设置文本到语音引擎的语言。
Method 13: setPitchAndSpeed(pitch: Float, speed: Float)设置了文本到语音引擎产生的语音的音调和速度。
Method 14: resetPitchAndSpeed()文本到语音引擎产生的语音的音调和速度重置为默认值。
如果您想为图书馆做出贡献,请提供存储库并创建拉动请求。
MIT License
Copyright (c) 2023 Ayaan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`