I am developing an android application that get a PDF file and extract its content and then convert this text to an audio file. I am using itext library to get parsed text and TTS Engine to save the audio file to specific directory through (synthesizeToFile) method. I think that every thing is OK but when i run the app i see an error and don't know what is that and how to solve it.
This is the error:
and here is my code:
Custom Adapter class:
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<PDFDoc> pdfDocs;
private TextToSpeech mTts;
private Uri fileUri;
private Uri sendFileUri;
public CustomAdapter(Context c, ArrayList<PDFDoc> pdfDocs) {
this.c = c;
this.pdfDocs = pdfDocs;
}
@Override
public int getCount() {
return pdfDocs.size();
}
@Override
public Object getItem(int i) {
return pdfDocs.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
//INFLATE CUSTOM LAYOUT
view = LayoutInflater.from(c).inflate(R.layout.model, viewGroup, false);
}
final PDFDoc pdfDoc = (PDFDoc) this.getItem(i);
TextView nameTxt = (TextView) view.findViewById(R.id.nameTxt);
ImageView img = (ImageView) view.findViewById(R.id.pdfImage);
//BIND DATA
nameTxt.setText(pdfDoc.getName());
img.setImageResource(R.drawable.pdf_icon);
//VIEW ITEM CLICK
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String received = extractPDF(pdfDoc.getPath());
sendFileUri=synthesizeToFile(received);
Intent intent = new Intent(c,PDFMediaPlayer.class);
intent.putExtra("PATH",pdfDoc.getPath());
intent.setData(sendFileUri);
c.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
});
return view;
}
//Extract PDF
public String extractPDF(String path) throws IOException {
String parsedText = "";
PdfReader reader = new PdfReader(path);
int n = reader.getNumberOfPages();
for (int page = 0; page < n; page++) {
parsedText = parsedText + PdfTextExtractor.getTextFromPage(reader, page + 1).trim() + "\n"; //Extracting the content from the different pages
}
reader.close();
return parsedText;
}
public Uri synthesizeToFile (String received) throws IOException {
HashMap<String, String> myHashRender = new HashMap();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, received);
String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File appTmpPath = new File(exStoragePath + "/sounds/");
appTmpPath.mkdirs();
//File file = new File();
String tempFilename = "tts.wav";
String tempDestFile = appTmpPath.getAbsolutePath() + "/" + tempFilename;
mTts.synthesizeToFile(received, myHashRender, tempDestFile);
File fileTTS = new File(tempDestFile);
fileUri = Uri.fromFile(fileTTS);
return Uri.parse(fileUri.getPath());
}
}
and here is my code for class PDFMediaPlayer:
public class PDFMediaPlayer extends AppCompatActivity {
private Button b1,b2,b3,b4;
private ImageView iv;
private double startTime = 0;
private double finalTime = 0;
private Handler myHandler = new Handler();;
private int forwardTime = 5000;
private int backwardTime = 5000;
private SeekBar seekbar;
private TextView tx1,tx2,tx3;
public static int oneTimeOnly = 0;
private MediaPlayer mediaPlayer;
private Uri mediaplayerUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_player);
Intent i = this.getIntent();
String path = i.getExtras().getString("PATH");
mediaplayerUri = i.getData();
File file = new File(path);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
tx1 = (TextView) findViewById(R.id.textView2);
tx2 = (TextView) findViewById(R.id.textView3);
tx3 = (TextView) findViewById(R.id.textView4);
tx3.setText(file.getName());
mediaPlayer = MediaPlayer.create(this,mediaplayerUri);
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setClickable(false);
b2.setEnabled(false);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Playing sound", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if (oneTimeOnly == 0) {
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
tx2.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)
finalTime)))
);
tx1.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)
startTime)))
);
seekbar.setProgress((int) startTime);
myHandler.postDelayed(UpdateSongTime, 100);
b2.setEnabled(true);
b3.setEnabled(false);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Pausing sound", Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
b2.setEnabled(false);
b3.setEnabled(true);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int) startTime;
if ((temp + forwardTime) <= finalTime) {
startTime = startTime + forwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(), "You have Jumped forward 5 seconds", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Cannot jump forward 5 seconds", Toast.LENGTH_SHORT).show();
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int) startTime;
if ((temp - backwardTime) > 0) {
startTime = startTime - backwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(), "You have Jumped backward 5 seconds", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Cannot jump backward 5 seconds", Toast.LENGTH_SHORT).show();
}
}
});
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mediaPlayer.getCurrentPosition();
tx1.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(this, 100);
}
};
}