Search code examples
androidmedia-playerseekbar

problem in using seekbar for android media player


in my app i am using media player and i want to place a seek bar there. In my code the song is playing but the seek bar is not moving. Following is the part of my code...

sv = (SurfaceView)findViewById(R.id.surfaceView1);
sv.postDelayed(runnable,200);  

 final Runnable runnable = new Runnable()
        {
            @Override
            public void run() 
            {
                if (mediaPlayer != null) 
                {
                    if (isPlaying) 
                    {
                        try 
                        {
                           int currentPosition = mediaPlayer.getCurrentPosition();

                            audioProgress.setProgress((int) ((currentPosition / (float) mediaPlayer.getDuration()) * 100f));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        sv.postDelayed(runnable, 150);
                    }
                }
            }

        };

Please anyone help me in clearing the errors...


Solution

  • Make these changes to your code:

    sv = (SurfaceView)findViewById(R.id.surfaceView1);
    audioProgress.setMax(mediaPlayer.getDuration()); // this is one change
    sv.postDelayed(runnable,200);  
    
     final Runnable runnable = new Runnable()
            {
                @Override
                public void run() 
                {
                    if (mediaPlayer != null) 
                    {
                        if (isPlaying) 
                        {
                            try 
                            {
                               int currentPosition = mediaPlayer.getCurrentPosition();
    
                                    // the other change:
                        audioProgress.setProgress(currentPosition);
    
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
    
                            sv.postDelayed(runnable, 150);
                        }
                    }
                }
    
            };