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>
이거 내 폰에다 해봣는데 계속 에러가 난다 보완필요
'System 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 |