728x90
반응형
1. AndroidManifest.xml 파일에 인터넷과 오디오 접근 허용을 위한 문장을 추가한다.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2. activity_main.xml 파일에 Textview 와 Button 을 하나씩 추가한다.
Textview : stt 결과를 출력하기 위함
Button : stt 명령을 시작하기 위함
<?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">
<TextView
android:id="@+id/sttResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STT 결과"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.405" />
<Button
android:id="@+id/sttStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="154dp"
android:layout_marginTop="62dp"
android:text="STT Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/sttResult" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity.java 파일에 STT 기능을 추가한다.
package com.example.testapp;
import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Intent intent;
SpeechRecognizer mRecognizer;
Button sttBtn;
TextView textView;
final int PERMISSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( Build.VERSION.SDK_INT >= 23 ){
// 퍼미션 체크
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET,
Manifest.permission.RECORD_AUDIO},PERMISSION);
}
textView = findViewById(R.id.sttResult);
sttBtn = findViewById(R.id.sttStart);
intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"ko-KR");
sttBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecognizer=SpeechRecognizer.createSpeechRecognizer(MainActivity.this);
mRecognizer.setRecognitionListener(listener);
mRecognizer.startListening(intent);
}
});
}
private RecognitionListener listener = new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
Toast.makeText(getApplicationContext(),"음성인식을 시작합니다.",Toast.LENGTH_SHORT).show();
}
@Override
public void onBeginningOfSpeech() {}
@Override
public void onRmsChanged(float rmsdB) {}
@Override
public void onBufferReceived(byte[] buffer) {}
@Override
public void onEndOfSpeech() {}
@Override
public void onError(int error) {
String message;
switch (error) {
case SpeechRecognizer.ERROR_AUDIO:
message = "오디오 에러";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "클라이언트 에러";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "퍼미션 없음";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "네트워크 에러";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "네트웍 타임아웃";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "찾을 수 없음";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RECOGNIZER가 바쁨";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "서버가 이상함";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "말하는 시간초과";
break;
default:
message = "알 수 없는 오류임";
break;
}
Toast.makeText(getApplicationContext(), "에러가 발생하였습니다. : " + message,Toast.LENGTH_SHORT).show();
}
@Override
public void onResults(Bundle results) {
ArrayList<String> matches =
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for(int i = 0; i < matches.size() ; i++){
textView.setText(matches.get(i));
}
}
@Override
public void onPartialResults(Bundle partialResults) {}
@Override
public void onEvent(int eventType, Bundle params) {}
};
}
4. 결과
https://medium.com/wasd/creating-an-android-google-stt-application-4cea24ee97af
Creating an Android Google STT application
STT(Speech to Text)로 음성인식 어플을 만들기
medium.com
위 블로그를 참고하여 작성하였습니다.
728x90
반응형
'IT > API' 카테고리의 다른 글
API 호출 시 파라미터가 없는 버그 (0) | 2022.09.27 |
---|---|
STT API 실행파일로 만들기!!! (0) | 2021.01.27 |
STT 과제 - Google STT 활용 (0) | 2021.01.27 |