1

TextToSpeechクラスを使用して、私のアプリ内のテキストを試してみようとしています。コードを実行すると何も聞こえませんが、音量は高いです。私のコードで何が間違っていますか?許可が必要なのですか?Androidのテキストから音声へ

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener { 

    TextToSpeech textToSpeech; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textToSpeech = new TextToSpeech(this, this); 

     speakOut(); 
    } 

    @Override 
    public void onInit(int Text2SpeechCurrentStatus) { 

     if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) { 

      int result = textToSpeech.setLanguage(Locale.US); 

      if (result == TextToSpeech.LANG_MISSING_DATA 
        || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.e("TTS", "This Language is not supported"); 
      } else { 
       speakOut(); 
      } 

     } else { 
      Log.e("TTS", "Initilization Failed!"); 
     } 
    } 

    private void speakOut() { 
     String g= "Hello"; 
     textToSpeech.speak(g, TextToSpeech.QUEUE_FLUSH, null); 
    } 
} 

答えて

0

これはコメントかもしれないが、私はわからないが、あなたは
if (Text2SpeechCurrentStatus != TextToSpeech.ERROR) {

に 変更
if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {
を試すことができ、多分あなたはText2SpeechCurrentStatusの値が何であるか、デバッグすることができます

1

まず、あなたのデバイスにTTSエンジンがインストールされているかどうか確認してください。

また、TTSを使用するための許可は必要ありません。

このようなアクティビティのonCreate()メソッドのTextToSpeechインスタンスを初期化します。

TextToSpeech t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { 
    @Override 
    public void onInit(int status) { 
     if(status != TextToSpeech.ERROR) { 
      t1.setLanguage(Locale.UK); 
     } 
    } 
    }); 

//これはspeakOut()メソッドです。

private void speakOut() { 
    String g= "Hello"; 
    t1.speak(g, TextToSpeech.QUEUE_FLUSH, null); 
} 

...それがお役に立てば幸い
関連する問題