8

I have set:

mSeekBar.setMax(mp.getDuration()); // 8480

After completion of Audiofile, What I am getting is:

 player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mediaPlayer)
                    {
                        Log.e("onComplete>>", ""+mediaPlayer.getCurrentPosition());

                        // mediaPlayer.getCurrentPosition() = 8192

                        Log.e("getDuration", ""+mediaPlayer.getDuration());

                        //  mediaPlayer.getDuration() = 8480

                       if(mediaPlayer.getCurrentPosition()>=mediaPlayer.getDuration())
                       {
                            // Why never get called???
                       }
                    }
                });

So, Why is MediaPlayer's Current position never reaches the total duration of Audio file ?

Or technically we can say as:

Why Not?

mediaPlayer.getCurrentPosition()==mediaPlayer.getDuration()

Why Always

mediaPlayer.getCurrentPosition() < mediaPlayer.getDuration() in OnCompletion listener?

For Example:

I have a Play Symbol for starting the Player. Now when I press play symbol it will convert to Pause symbol.

I have a Maxduration of audiodfile.

Now I want to convert Pause symbol to Play Symbol when Audio file is played completely. SO what I am doing is Checking:

if(mediaPlayer.getCurrentPosition()>=mediaPlayer.getDuration())
{
       //  Convert Imagview from Pause to Play    
      // But never get called                   
}
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
  • What's output of `Log.i("onComplete>>", ""+mediaPlayer.getCurrentPosition()); Log.i("getDuration", ""+mediaPlayer.getDuration());` – MysticMagicϡ Sep 26 '14 at 10:31
  • current position is always less than GetDuration. – Sagar Shah Sep 26 '14 at 10:32
  • 1
    Hmm.. I found an [issue thread](https://code.google.com/p/android/issues/detail?id=38627) for same, but don't know if anyone found solution or explanation yet.. and [this](http://www.anddev.org/multimedia-problems-f28/mediaplayer-getcurrentposition-t20032.html), as well. – MysticMagicϡ Sep 26 '14 at 10:41
  • Yeah I will try it out by running another thread at the same time.But its still strange that OnCompletionListener() also don't provide correct value for getCurrentPosition(). – Sagar Shah Sep 26 '14 at 10:53
  • I am having a hunch that in `onCompletion` the track is already completed, so may be reset to starting position, so its not same as total duration. But its just a wild guess. :) – MysticMagicϡ Sep 26 '14 at 10:54
  • No,I have tested getDuration in OnSeekBarchangeListener and the runnable itself that getCurrentPosition() never get >= getDuration() – Sagar Shah Sep 26 '14 at 10:58

1 Answers1

0

If you need to do some task on completion by checking currentPoision to duration, you can do below trick:

if(mediaPlayer.getDuration()-mediaPlayer.getCurrentPosition()<1000){//milliseconds
  new Handler().postDelayed(
     new Runnable() {
         @Override
         public void run() {
             // Convert Imageview from Pause to Play    
         }
     },1000); 
}
Idan
  • 5,405
  • 7
  • 35
  • 52
support_ms
  • 1,873
  • 1
  • 18
  • 33