SpeechSynthesisActivity.java
Dosyayı İndir
package com.godoro.androidmedia;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.speech.tts.Voice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Locale;
public class SpeechSynthesisActivity extends AppCompatActivity {
private Button speakButton;
private TextView textEdit;
private TextToSpeech textToSpeech;
private Locale turkishLocale = new Locale("tr", "TR");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speech_synthesis);
speakButton=findViewById(R.id.speakButton);
textEdit=findViewById(R.id.textEdit);
textEdit.setText("Godoro Android'i Konuşturuyor!");
speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
speak();
}
});
createTextToSpeech();
}
private void createTextToSpeech(){
TextToSpeech.OnInitListener listener = new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
initializeTextToSpeech(status);
}
};
textToSpeech= new TextToSpeech(this, listener);
//textToSpeech = new TextToSpeech(this, listener, "com.google.android.tts");
textToSpeech.setPitch(1);
logVoices();
}
private void initializeTextToSpeech(int status){
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(turkishLocale);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
String message = "Desteklenmeyen yazıdan konuşma dili";
Toast.makeText(SpeechSynthesisActivity.this, message, Toast.LENGTH_LONG).show();
Log.e("GodoroAndroid", message);
} else {
String message = "Yazıdan konuşma yapılıyor";
Toast.makeText(SpeechSynthesisActivity.this, message, Toast.LENGTH_LONG).show();
Log.i("GodoroAndroid", message);
}
} else {
String message = "Yazıdan konuşma başarısız";
Toast.makeText(SpeechSynthesisActivity.this, message, Toast.LENGTH_LONG).show();
Log.e("GodoroAndroid", message);
}
}
private void logVoices() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (textToSpeech.getVoices() != null) {
for (Voice voice : textToSpeech.getVoices()) {
Log.i("GodoroAndroid", "Ses : "+voice.getName());
}
}
}
}
@Override
public void onDestroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
private void speak() {
String text = textEdit.getText().toString();
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
Dosyayı İndir