1. SpeechToText
페이지 정보
작성자 관리자 댓글 0건 조회 3,141회 작성일 20-02-14 10:42본문
1. SpeechToText
1. 프로젝트 생성
프로젝트명 : SpeechToText
2. 화면 디자인 수정 : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="0dp"
android:text=""
app:layout_constraintBottom_toTopOf="@+id/btnSpeak"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/btnSpeak"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/speak" />
<ImageView
android:id="@+id/btnClear"
android:onClick="onClickClear"
android:layout_width="50dp"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/clear" />
<ImageView
android:id="@+id/btnSave"
android:layout_width="50dp"
android:layout_height="50dp"
android:onClick="onClickSave"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/save" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. 이미지 복사
4. MainActivity.kt 수정
package kr.co.leelab.speechtotext
import android.Manifest
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import android.content.ActivityNotFoundException
import android.speech.RecognizerIntent
import android.view.ContextThemeWrapper
import java.util.*
class MainActivity : AppCompatActivity() {
var tv : TextView? = null
val REQ_CODE:Int = 100
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv = findViewById<TextView>(R.id.tv)
checkPermission()
}
private fun checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + packageName))
startActivity(intent)
finish()
Toast.makeText(this, "Enable Microphone Permission..!!", Toast.LENGTH_SHORT).show()
}
}
}
fun onClick(view: View) {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Need to speak")
try {
startActivityForResult(intent, REQ_CODE)
} catch (a: ActivityNotFoundException) {
Toast.makeText(
applicationContext,
"Sorry your device not supported",
Toast.LENGTH_SHORT
).show()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
REQ_CODE -> {
if (resultCode == Activity.RESULT_OK && null != data) {
val result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
val txt = tv?.getText() as String
tv?.setText(txt + "\n" + result!![0] as CharSequence)
}
}
}
}
fun onClickClear(view: View) {
val builder = AlertDialog.Builder(ContextThemeWrapper(this, R.style.Theme_AppCompat_Light_Dialog))
builder.setTitle("메모 삭제하기")
builder.setMessage("메모를 삭제하시겠습니까?")
builder.setIcon(R.drawable.clear)
builder.setPositiveButton("확인") { _, _ ->
// 확인시 처리 로직
tv?.setText("")
}
builder.setNegativeButton("취소") { _, _ ->
// 취소시 처리 로직
}
builder.show()
}
fun onClickSave(view: View) {
val builder = AlertDialog.Builder(ContextThemeWrapper(this, R.style.Theme_AppCompat_Light_Dialog))
builder.setTitle("메모 저장하기")
builder.setMessage("메모를 저장시겠습니까?")
builder.setIcon(R.drawable.save)
builder.setPositiveButton("확인") { _, _ ->
// 확인시 처리 로직
}
builder.setNegativeButton("취소") { _, _ ->
// 취소시 처리 로직
}
builder.show()
}
}
5. 권한 설정 : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.leelab.speechtotext">
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<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/AppTheme">
<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>
6. 프로젝트 실행
참고
댓글목록
등록된 댓글이 없습니다.