前提・実現したいこと
Androidで,アプリを開き,ボタンを押すと音声認識のモードに遷移し,ユーザが何かしゃべるとAndroidが音声認識をし,画面にテキストとして表示する,というようなアプリを実現したいです.現在,日本語と英語で実現することができたのですが,中国語で実現することができません.Androidをオフラインにした状態でもアプリが動くように,言語のダウンロードをしてあります.
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
エラーは発生しないのですが,中国語の音声認識の際に,中国語だけでなく日本語も認識してしまいます.例えば,「こんにちは」という言葉は日本語なので中国語の音声認識では反応しないはずが,中国語の音声認識で反応してしまいます.中国語の音声認識の際には,中国語だけに反応するようにしたいです.
該当のソースコード
MainActivity
1package com.example.keita.gtts; 2import android.content.Intent; 3import android.speech.RecognitionListener; 4import android.speech.RecognizerIntent; 5import android.speech.SpeechRecognizer; 6import android.speech.tts.TextToSpeech; 7import android.support.v7.app.AppCompatActivity; 8import android.os.Bundle; 9import android.view.View; 10import android.widget.Button; 11import android.widget.EditText; 12import android.widget.TextView; 13import android.widget.Toast; 14import java.util.ArrayList; 15import java.util.Locale; 16 17public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener { 18 private Button button_jpn; 19 private Button button_en; 20 private Button button_ch; 21 private EditText editText; 22 private TextToSpeech tts; 23 private String text_jpn; 24 private SpeechRecognizer mRecognizer; 25 private TextView textView; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 button_jpn = findViewById(R.id.button_jpn); 32 button_en = findViewById(R.id.button_en); 33 button_ch = findViewById(R.id.button_ch); 34 editText = findViewById(R.id.editText); 35 textView = findViewById(R.id.textView); 36 //tts = new TextToSpeech(this, this); 37 mRecognizer = SpeechRecognizer.createSpeechRecognizer(this); 38 mRecognizer.setRecognitionListener(new listener()); 39 button_jpn.setOnClickListener(new View.OnClickListener() { 40 @Override 41 public void onClick(View v1) { 42 Intent intent1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 43 intent1.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ja-JP"); 44 intent1.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true); 45 intent1.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName()); 46 mRecognizer.startListening(intent1); 47 //speechText(); 48 } 49 }); 50 button_en.setOnClickListener(new View.OnClickListener() { 51 @Override 52 public void onClick(View v2) { 53 Intent intent2 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 54 intent2.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.US.toString()); 55 intent2.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true); 56 intent2.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName()); 57 mRecognizer.startListening(intent2); 58 //speechText(); 59 } 60 }); 61 button_ch.setOnClickListener(new View.OnClickListener() { 62 @Override 63 public void onClick(View v3) { 64 Intent intent3 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 65 intent3.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "zh-Hans-CN"); 66 intent3.putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true); 67 intent3.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName()); 68 mRecognizer.startListening(intent3); 69 //speechText(); 70 } 71 }); 72 } 73 @Override 74 public void onInit(int status) { 75 } 76 /* 77 private void speechText(){ 78 text_jpn = editText.getText().toString(); 79 tts.speak(text_jpn, TextToSpeech.QUEUE_FLUSH, null, null); 80 } 81 */ 82 83 public class listener implements RecognitionListener{ 84 @Override 85 public void onReadyForSpeech(Bundle params) { 86 Toast.makeText(getApplicationContext(), "準備終了", Toast.LENGTH_SHORT).show(); 87 } 88 @Override 89 public void onBeginningOfSpeech() { 90 Toast.makeText(getApplicationContext(), "入力開始", Toast.LENGTH_SHORT).show(); 91 } 92 @Override 93 public void onRmsChanged(float rmsdB) { 94 } 95 @Override 96 public void onBufferReceived(byte[] buffer) { 97 } 98 @Override 99 public void onEndOfSpeech() { 100 Toast.makeText(getApplicationContext(), "入力終了", Toast.LENGTH_SHORT).show(); 101 } 102 @Override 103 public void onError(int error) { 104 } 105 @Override 106 public void onResults(Bundle results) { 107 String key = SpeechRecognizer.RESULTS_RECOGNITION; 108 ArrayList<String> list = results.getStringArrayList(key); 109 String str = ""; 110 for (String s : list){ 111 str += s; 112 } 113 textView.setText(str); 114 } 115 @Override 116 public void onPartialResults(Bundle partialResults) { 117 } 118 @Override 119 public void onEvent(int eventType, Bundle params) { 120 } 121 } 122}
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <Button 10 android:id="@+id/button_jpn" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_marginEnd="8dp" 14 android:layout_marginStart="8dp" 15 android:layout_marginTop="24dp" 16 android:text="Japanese" 17 app:layout_constraintEnd_toEndOf="parent" 18 app:layout_constraintHorizontal_bias="0.501" 19 app:layout_constraintStart_toStartOf="parent" 20 app:layout_constraintTop_toBottomOf="@+id/editText" /> 21 22 <EditText 23 android:id="@+id/editText" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_marginEnd="8dp" 27 android:layout_marginStart="8dp" 28 android:layout_marginTop="100dp" 29 android:ems="10" 30 android:hint="翻訳するテキストを入力" 31 android:inputType="text" 32 app:layout_constraintEnd_toEndOf="parent" 33 app:layout_constraintStart_toStartOf="parent" 34 app:layout_constraintTop_toTopOf="parent" /> 35 36 <TextView 37 android:id="@+id/textView" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:layout_marginEnd="8dp" 41 android:layout_marginStart="8dp" 42 android:layout_marginTop="20dp" 43 app:layout_constraintEnd_toEndOf="parent" 44 app:layout_constraintStart_toStartOf="parent" 45 app:layout_constraintTop_toBottomOf="@+id/button_ch" /> 46 47 <Button 48 android:id="@+id/button_en" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:layout_marginEnd="8dp" 52 android:layout_marginStart="8dp" 53 android:layout_marginTop="20dp" 54 android:text="English" 55 app:layout_constraintEnd_toEndOf="parent" 56 app:layout_constraintStart_toStartOf="parent" 57 app:layout_constraintTop_toBottomOf="@+id/button_jpn" /> 58 59 <Button 60 android:id="@+id/button_ch" 61 android:layout_width="wrap_content" 62 android:layout_height="wrap_content" 63 android:layout_marginEnd="8dp" 64 android:layout_marginStart="8dp" 65 android:layout_marginTop="20dp" 66 android:text="China" 67 app:layout_constraintEnd_toEndOf="parent" 68 app:layout_constraintStart_toStartOf="parent" 69 app:layout_constraintTop_toBottomOf="@+id/button_en" /> 70 71</android.support.constraint.ConstraintLayout>
Manifest
1 <uses-permission android:name="android.permission.INTERNET"/> 2 <uses-permission android:name="android.permission.RECORD_AUDIO"/>
試したこと
中国語のLocaleについて調べ,ソースコードに記載した形にしたのですが,うまくいきませんでした.
補足情報(FW/ツールのバージョンなど)
Android Studio 3.1.4
Windows 10
Nexus 5X
あなたの回答
tips
プレビュー