Google Android文本到语音不使用Android上的PDFBox

发布于 2025-01-18 09:52:46 字数 5712 浏览 1 评论 0原文

我使用 PDFBox 从文档中提取文本并显示它,我还希望提取的文本可用于文本转语音,以便它可以口述它,但它不起作用。我尝试了 Stackoverflow 和其他网站上不同论坛的不同方法。不幸的是所有这些都不起作用。已经过去很长时间了,这个问题仍然没有得到解决。我很感激你们!我的代码是用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 是这样的..

<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" />

我确实尝试过

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文