1

내 앱에서 텍스트를 말하기 위해 TextToSpeech 클래스를 사용하려고합니다. 코드를 실행할 때 아무 것도 듣지 못하면 볼륨이 높습니다. 내 코드에 어떤 문제가 있습니까? 허가가 필요합니까?Android text to speech

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); 
} 

... 희망이 도움이