Android Text 를 Speech 로 변환하는 예제다.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".TextToSpeechActivity">
<com.google.android.material.appbar.AppBarLayout android:id="@+id/layout_appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.textfield.TextInputLayout android:id="@+id/layout_textvoice" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="30dp" android:layout_marginEnd="24dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/layout_appbar">
<com.google.android.material.textfield.TextInputEditText android:id="@+id/et_text2voice" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="텍스트를 입력하세요" android:padding="10dp"></com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton android:id="@+id/btn_speech" style="@style/Widget.AppCompat.Button.Borderless.Colored" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="30dp" android:layout_marginEnd="24dp" android:padding="10dp" android:text="음성변환" android:textColor="@color/colorOrangeDark" android:textSize="16sp" android:textStyle="bold" app:backgroundTint="@color/colorSkyBlue" app:cornerRadius="15dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/layout_textvoice" />
</androidx.constraintlayout.widget.ConstraintLayout> |
import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar;
import java.util.Locale;
public class TextToSpeechActivity extends AppCompatActivity { private final String TAG = this.getClass().getSimpleName(); Context mContext;
private TextToSpeech textToSpeech; private EditText speakText; private Button speakBtn;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_speech); mContext = TextToSpeechActivity.this;
initView(); }
private void initView() { Toolbar toolbar = findViewById(R.id.toolbar); toolbar.setTitle("TextToSpeech"); setSupportActionBar(toolbar);
speakText = findViewById(R.id.et_text2voice); speakBtn = findViewById(R.id.btn_speech);
textToSpeech = new TextToSpeech(getApplicationContext(), status -> { if (status != TextToSpeech.ERROR) { textToSpeech.setLanguage(Locale.KOREAN); } });
speakBtn.setOnClickListener(v -> texttoSpeak()); }
private void texttoSpeak() { String text = speakText.getText().toString(); if ("".equals(text)) { text = "Please enter some text to speak."; Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); return; }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null); } else { String utteranceId = this.hashCode() + ""; textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId); } }
@Override protected void onDestroy() { if (textToSpeech != null) { textToSpeech.stop(); textToSpeech.shutdown(); } super.onDestroy(); }
@Override public void onBackPressed() { super.onBackPressed(); startActivity(new Intent(mContext,SplashActivity.class)); finish(); } }
|
예제 전체 소스코드는 https://github.com/jsk005/JavaProjects/tree/master/speechtext 에 있다.