TextToSpeech에서 텍스트를 음성으로 바꾸는 간단한 사용
2953 단어 android
main.xml
MainAty.java
package fly.aty;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainAty extends Activity implements OnClickListener, OnInitListener{
private Button speechBtn; //
private TextView speechTxt; //
private TextToSpeech textToSpeech; // TTS
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
speechBtn = (Button) findViewById(R.id.speechBtn);
speechBtn.setOnClickListener(this);
speechTxt = (TextView) findViewById(R.id.speechTxt);
textToSpeech = new TextToSpeech(this, this); // Context,TextToSpeech.OnInitListener
}
/**
* TextToSpeech
* status:SUCCESS ERROR 2
* setLanguage , 22
* TextToSpeech.LANG_MISSING_DATA: 。
* TextToSpeech.LANG_NOT_SUPPORTED:
*/
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View v) {
if (textToSpeech != null && !textToSpeech.isSpeaking()) {
textToSpeech.setPitch(0.5f);// , ( ), ,1.0
textToSpeech.speak(speechTxt.getText().toString(),
TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
protected void onStop() {
super.onStop();
textToSpeech.stop(); // TTS
textToSpeech.shutdown(); // ,
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.