如何在已经加载到PDFView的PDF上实现文本到语音的转换

时间:2018-08-20 14:28:33

标签: android text-to-speech pdfview

我正在尝试在我的应用程序中实现文本语音转换。首先,我实现了PDFview,使我可以从设备中选择PDF文件并加载它。我要实现的是,在将此PDF加载到PDFview之后,我想读出文本。我该如何实现?

使用传递到我的PDFView libabry中的同一Uri加载pdf,以便我可以查看

public String getRealPathFromURI(Context context, Uri contentURI)
    {
        String result;

        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if(cursor == null)
        {
            result = contentURI.getPath();
        } else {

            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
}








 btnSpeak.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
                    String parsedText = "";
               try
               {

                    PdfReader pdfReader = new  PdfReader(getRealPathFromURI(getContext(), uri));
                   int n = pdfReader.getNumberOfPages();
                  for (int i = 0; i < n; i++) {
                       parsedText = parsedText + PdfTextExtractor.getTextFromPage(pdfReader, i + 1).trim() + "\n";
                   }
                   pdfReader.close();


                   textToSpeech.speak(parsedText, TextToSpeech.QUEUE_FLUSH, null);
               }
               catch (Exception ex)
               {

               }
           }
       });

上面的代码是我试图做的。

1 个答案:

答案 0 :(得分:0)

尝试一下:

btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String parsedText = "We did not get any text from the PDF.  You will have to examine why.";
                try {
                    PdfReader pdfReader = new PdfReader(getRealPathFromURI(getContext(), uri));
                    int n = pdfReader.getNumberOfPages();
                    for (int i = 0; i < n; i++) {
                        parsedText = parsedText + PdfTextExtractor.getTextFromPage(pdfReader, i + 1).trim() + "\n";
                    }
                    pdfReader.close();
                    } 
                    catch (Exception ex) 
                    {
                    Log.i("XXX", "There was an exception: " + ex);    
                    }
                textToSpeech.speak(parsedText, TextToSpeech.QUEUE_FLUSH, null);
            }
        });