How to Convert Text to Speech Voice or Audio Mp3 App in Android Studio Using Java Full Project For Beginners

 

Welcome folks today in this blog post we will be converting text to speech voice or audio mp3 in android studio using java.All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

java code

 

 

package com.example.mysampletexttospeech;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private EditText editText;
private TextToSpeech textToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText = findViewById(R.id.editText);
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
textToSpeech.setLanguage(Locale.US);
textToSpeech.setSpeechRate((float) 2.5);

}
});
}

public void TextToSpeechButton(View view){
textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null,null);
}
}

 

 

 

xml code

 

 

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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”>

<EditText
android:id=”@+id/editText”
android:layout_width=”360dp”
android:layout_height=”wrap_content”
android:layout_marginStart=”20dp”
android:layout_marginTop=”150dp”
android:ems=”10″
android:gravity=”start|top”
android:hint=”@string/your_text_here”
android:inputType=”textMultiLine”
android:autofillHints=”” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”120dp”
android:layout_marginTop=”60dp”
android:onClick=”TextToSpeechButton”
android:text=”@string/text_to_speech_convert” />
</RelativeLayout>

 

 

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mysampletexttospeech”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

Leave a Reply