1000sj
SJ CODE
1000sj
전체 방문자
오늘
어제
  • 분류 전체보기
    • 알고리즘
    • 네트워크 보안
      • 네트워크
      • 보안
      • CTF
      • Exploit
    • System Programming
      • Operating System
      • Compiler
      • Device Driver
      • Emulator
    • Application Programming
      • Script
      • Android
    • 클라우드 컴퓨팅
      • Cloud Native
      • Public Cloud
      • Infrastructure
      • Database
      • DevOps
    • 트러블슈팅
    • ETC
      • 문화 생활
      • 커뮤니티

인기 글

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
1000sj

SJ CODE

[Android/Java] 음성인식 해서 글로 변환
Application Programming/Android

[Android/Java] 음성인식 해서 글로 변환

2021. 6. 14. 02:13

Demo

MainActivity.java

package com.example.recyclerview;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    protected static final int RESULT_SPEECH = 1;
    private ImageButton btnSpeak;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text_view);
        btnSpeak = findViewById(R.id.btn_speak);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"ko-KR");
                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    textView.setText("");
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getApplicationContext(), "Your device doesn't support speech to text", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }/* catch (NullPointerException e) {
                    e.printStackTrace();
                }*/
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case RESULT_SPEECH:
                if(resultCode == RESULT_OK && data != null){
                    ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    textView.setText(text.get(0));
                }
                break;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">


    <TextView
        android:id="@+id/text_view_01iew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="18sp"/>

    <ImageButton
        android:id="@+id/btn_speak"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:srcCompat="@android:drawable/ic_btn_speak_now" />


</LinearLayout>

이거 내 폰에다 해봣는데 계속 에러가 난다 보완필요

'Application Programming > Android' 카테고리의 다른 글

[Android/Java] File stream 외부 Storage에 글 읽고 쓰기  (0) 2021.06.14
[Android/Java] Text To Speech  (0) 2021.06.14
[Android/Java] Sdcard에 이미지 다운로드  (0) 2021.06.14
[Android/Java] Image download using AsyncTask  (0) 2021.06.14
[Android/Java] bitmap 예제(00)  (0) 2021.06.13
    'Application Programming/Android' 카테고리의 다른 글
    • [Android/Java] File stream 외부 Storage에 글 읽고 쓰기
    • [Android/Java] Text To Speech
    • [Android/Java] Sdcard에 이미지 다운로드
    • [Android/Java] Image download using AsyncTask
    1000sj
    1000sj

    티스토리툴바