1 year ago
#367234
danfabros
Google Android Text-to-Speech is not working with PDFBox on Android
I use PDFBox to extract text from a document and display it, I also want the text extracted available to Text-to-Speech so it can dictate it but it does not work. I tried different methods from different forums on Stackoverflow and other websites. Unfortunately all of those doesn't work neither. It's been long since this problem is still unresolved. I appreciate y'all! My code is written in Java.
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
import com.tom_roush.pdfbox.pdmodel.PDDocument;
import com.tom_roush.pdfbox.text.PDFTextStripper;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class ReadPDF extends AppCompatActivity {
TextToSpeech tts;
TextView outputTextView;
private static final int READ_REQUEST_CODE = 42;
Uri fileUri;
Intent intentOpenFile;
String googleTtsPackage = "com.google.android.tts.service.GoogleTTSService";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
outputTextView = findViewById(R.id.output_text);
outputTextView.setMovementMethod(new ScrollingMovementMethod());
intentOpenFile = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intentOpenFile.setType("application/pdf");
startActivityForResult(intentOpenFile, READ_REQUEST_CODE);
tts = new TextToSpeech(this, status -> {
tts.setLanguage(Locale.forLanguageTag("fil-PH"));
tts.setSpeechRate(0.8F);
tts.setPitch(0.8F);
Locale[] locales = Locale.getAvailableLocales();
List<Locale> localeList = new ArrayList<>();
for (Locale locale : locales) {
int res = tts.isLanguageAvailable(locale);
if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
localeList.add(locale);
} else {
tts.setLanguage(Locale.US);
}
}
if(status != TextToSpeech.ERROR){
if(!isPackageInstalled(getPackageManager(), googleTtsPackage))
confirmDialog();
else tts.setEngineByPackageName(googleTtsPackage);
}
});
PDFBoxResourceLoader.init(getApplicationContext());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (resultData != null) {
fileUri = resultData.getData();
readPDFFile(fileUri);
Toast.makeText(this, "Loaded successfully", Toast.LENGTH_SHORT).show();
}
}
}
public void readPDFFile(Uri uri) {
PDDocument document = null;
String parsedText = null;
try {
InputStream inputStream = this.getContentResolver().openInputStream(uri);
document = PDDocument.load(inputStream);
PDFTextStripper pdfStripper = new PDFTextStripper();
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(20);
parsedText = pdfStripper.getText(document);
outputTextView.setText(parsedText);
tts.speak(parsedText, TextToSpeech.QUEUE_ADD, null, null);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onPause(){
super.onPause();
tts.stop();
}
@Override
public void onDestroy(){
super.onDestroy();
tts.shutdown();
}
private void confirmDialog(){
Intent installVoice = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installVoice);
}
public static boolean isPackageInstalled(PackageManager pm, String packageName) {
try {
pm.getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
}```
XML Activity is this..
<TextView
android:id="@+id/output_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:scrollbars="vertical"
android:text=""
android:textColor="#000000"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck"
tools:visibility="visible" />
I did try
public void readPDFFile(Uri uri) {
PDDocument document = null;
String parsedText = null;
try {
InputStream inputStream = this.getContentResolver().openInputStream(uri);
document = PDDocument.load(inputStream);
PDFTextStripper pdfStripper = new PDFTextStripper();
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(20);
parsedText = pdfStripper.getText(document);
outputTextView.setText(parsedText);
tts.speak(parsedText, TextToSpeech.QUEUE_ADD, null, null);
} catch (IOException e) { e.printStackTrace(); } }
and I expected it to give me Text-to-Speech output.
java
android
android-studio
text-to-speech
google-text-to-speech
0 Answers
Your Answer