Professional Documents
Culture Documents
Task 13.......
Task 13.......
Task 13.......
• Code:==
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text To Speech"
android:textColor="@color/white"
android:textSize="28sp"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:layout_marginTop="40sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/et_chats_enter_your_text"
android:hint="Enter your text"
android:textColorHint="@color/white"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:layout_margin="16dp"
android:padding="16dp"
android:gravity="center"
android:inputType="textMultiLine"
android:background="@drawable/edittext_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose the pitch"
android:textColor="@color/white"
android:textSize="16sp"
android:fontFamily="sans-serif-condensed"
android:layout_margin="16dp"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_chats_pitch"
android:min="0"
android:max="100"
android:progress="50"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose the speed"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/white"
android:fontFamily="sans-serif-condensed"
android:layout_margin="16dp"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_chats_speed"
android:min="0"
android:max="100"
android:indeterminateTint="@color/white"
android:thumbTint="@color/white"
android:progress="50"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_chats_speak"
android:text="Speak"
android:textColor="@color/yellow"
android:backgroundTint="@color/white"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>
</LinearLayout>
2. Chat Fragment.Java:--
package com.example.aniketsapp;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import java.util.Locale;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_chats, container,
false);
et_enter_your_text =
view.findViewById(R.id.et_chats_enter_your_text);
sb_pitch = view.findViewById(R.id.sb_chats_pitch);
sb_speed = view.findViewById(R.id.sb_chats_speed);
btn_speak = view.findViewById(R.id.btn_chats_speak);
btn_speak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speakText();
}
});
}
});
return view;
}
private void speakText() {
String text = et_enter_your_text.getText().toString();
if (text.isEmpty() || "".equals(text))
{
text = "Please Enter Some Text to Speak";
}
float pitch = (float) sb_pitch.getProgress()/50;
if (pitch <0.1)
{
pitch = 0.1f;
}
textToSpeech.setPitch(pitch);
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
}
}
Output:==