5분 안에 간단한 TextToSpeech 유틸리티 앱 만들기

5935 단어
  • 빈 활동이 있는 프로젝트를 만듭니다.
  • MainActivity에서 내장 클래스를 구현하십시오. TextToSpeech.OnInitListener

  • 다음과 같이 보일 것입니다.

    public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener 
    


  • activity_main의 코드 아래에 다음 코드 붙여넣기를 사용하여 표지 디자인을 만듭니다.

  • <?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=".MainActivity">
    
        <TextView
            android:id="@+id/txt_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:text="@string/text_to_speech"
            android:textColor="#000000"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <EditText
            android:id="@+id/et_input_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/rectangle"
            android:ems="10"
            android:hint="@string/enter_some_text"
            android:inputType="textPersonName"
            android:padding="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_label" />
    
        <Button
            android:id="@+id/btn_speak_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:text="@string/speak_now"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/et_input_text" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    


  • 이제 다음 리소스를 추가하여 activity_main에서 오류를 제거합니다
  • .
    strings.xml
        <string name="app_name">CodeWithRish</string>
        <string name="text_to_speech">Text To Speech</string>
        <string name="enter_some_text">Enter Some Text ......</string>
        <string name="speak_now">Speak Now</string>
    


  • 드로어블에 파일rectangle.xml을 생성하고 코드 아래에 붙여넣기

  • <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
        <stroke android:width="2dp" android:color="#03A9F4" />
        <padding android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp" />
        <corners android:radius="5dp" />
        <solid android:color="#ffffffff" />
    </shape>
    


  • MainActivity.java를 열고 코드를 다음으로 바꿉니다.

  • import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.speech.tts.TextToSpeech;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import java.util.Locale;
    
    public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    
        private TextToSpeech textToSpeech;
        private EditText etInputText;
        private Button btnSpeakText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            init();
    
            btnSpeakText.setOnClickListener(click -> {
                speakText();
            });
        }
    
        private void speakText() {
            String input = etInputText.getText().toString();
            textToSpeech.speak(input, TextToSpeech.QUEUE_FLUSH, null);
        }
    
        private void init() {
            textToSpeech = new TextToSpeech(this, this);
            etInputText = findViewById(R.id.et_input_text);
            btnSpeakText =  findViewById(R.id.btn_speak_text);
        }
    
        @Override
        public void onInit(int state) {
            if (state == TextToSpeech.SUCCESS) {
                int result = textToSpeech.setLanguage(Locale.UK);
    
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Toast.makeText(this, "Language Not Supported", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Enter Text", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
    


    이제 동일한 색상을 원하면 colorPrimarycolorPrimaryVariant를 대체할 수 있습니다.#FFEB3B 색상 코드가 있는 색상.

    좋은 웹페이지 즐겨찾기