안드로이드 애플리케이션에 텍스트 음성 기능을 제공하는 간단하고 효율적인 라이브러리.
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() TextView의 텍스트를 강조합니다.
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) OnHighlight 리스너를 설정합니다.
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.
`