Search code examples
androidandroid-mediaplayer

MediaPlayer getDuration returns -1412558917


I'm totally lost in this. I found the code pretty simple, and didn't find in the documentation any reason why would return a negative number that large. The code is as follows

private int getDuration(String audioPath) throws Exception {
   mediaPlayer = new MediaPlayer(); // This variable was created globally
   mediaPlayer.setDataSource(audioPath);
   return mediaPlayer.getDuration(); // Here it returns -1412558917 in every audio file i record, no matter if it's 3 seconds long or 8

}

I think that it doesn't matter how I record it, but if I'm mistaken I'll add the code. The format I save the file is 3gp. I'm able to reproduce the audio after this, so I don't know what might be the problem

Thanks


Solution

  • Have you tried adding mediaPlayer.prepare(); before you return the duration?