Search code examples
androidmultithreadingmedia-playerseekbar

How to show seekbar along with the custom mediaplayer


i have build a custom media player and i want to show the seekbar also. The seekbar is showing for now but is not functional i.e. it is not progressing when the song is played. Let me know where am i wrong . Following is the code

public class myPlayer extends Activity implements Runnable,SeekBar.OnSeekBarChangeListener {

    MediaPlayer mPlayer;
    Button play,stop;
    SeekBar seekbar;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        seekbar = (SeekBar)findViewById(R.id.seekbar);
        mPlayer = MediaPlayer.create(this,R.raw.sajnave);
        play=(Button)findViewById(R.id.play);
        stop=(Button)findViewById(R.id.stop);
        Toast.makeText(myPlayer.this, ""+mPlayer.getDuration(), Toast.LENGTH_SHORT).show();

        play.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mPlayer.start();

            }

        });

        stop.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(myPlayer.this, ""+mPlayer.getCurrentPosition(), Toast.LENGTH_SHORT).show();
                mPlayer.stop();
            }

        });
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        int currentPosition = 0;

                int total = mPlayer.getDuration();

                seekbar.setMax(total);

                while (mPlayer != null && currentPosition < total) {

                    try {

                        Thread.sleep(1000);

                        currentPosition = mPlayer.getCurrentPosition();

                    } catch (InterruptedException e) {

                        return;

                    } catch (Exception e) {

                        return;

                    }

                    seekbar.setProgress(currentPosition);

                }

    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        mPlayer.seekTo(arg1);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

}

Solution

  • Call updatePosition() method after mediplayer starts..It works for me.

    private final Handler handler = new Handler();
    
    private final Runnable updatePositionRunnable = new Runnable() {
        public void run() {
            updatePosition();
        }
    };
    
    private void updatePosition() {
        handler.removeCallbacks(updatePositionRunnable);
        if (player != null) seekbar.setProgress(player.getCurrentPosition());
    
        handler.postDelaye(updatePositionRunnable, 500);
    
    }
    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        player.seekTo(arg1);
    }
    
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
    
    }