Text To Speech는 안드로이드에서 제공하는 텍스트 음성변환 기능이다.
아래 영상과 같이 텍스트 입력 부분에 글을 입력하고
말하기 버튼을 누르면 음성으로 출력해준다!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="46dp"
android:width="300dp"
android:gravity="center"
android:hint="Enter Text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="46dp"
android:text="말하기" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.ttspractice
import android.os.Bundle
import android.speech.tts.TextToSpeech
import androidx.appcompat.app.AppCompatActivity
import com.example.ttspractice.databinding.ActivityMainBinding
import java.util.*
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
var tts: TextToSpeech? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
tts = TextToSpeech(
applicationContext
) { status ->
if (status != TextToSpeech.ERROR) {
tts!!.language = Locale.KOREAN
}
}
binding.button.setOnClickListener {
val toSpeak = binding.editText.text.toString()
tts!!.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null)
}
}
public override fun onPause() {
if (tts != null) {
tts!!.stop()
tts!!.shutdown()
}
super.onPause()
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.ttspractice">
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TTSPractice"
tools:targetApi="m">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Android 11을 타겟팅 하는 경우 AndroidManifest에서 INTENT_ACTION_TTS_SERVICE을 반드시 선언해야 한다.(Line 6~10)
그렇지 않으면 소리가 나지 않는다.
전체 코드
aerimforest/TTS_Practice
Contribute to aerimforest/TTS_Practice development by creating an account on GitHub.
github.com
반응형
'Android' 카테고리의 다른 글
getJSONArray vs OptJSONArray (0) | 2023.06.18 |
---|