0

In my application, I am streaming a Mp3 files from the server. Streaming and load working perfectly . But the problem in the seekto in the media player,

like I have 2 min mp3 ,if I have loaded it and play ,guess buffering go to the 100%, after playing it 50% (means approx 1 min), we seekto it 25% (means at 30 sec) , the player will starting from the 10 sec , it is very odd behavior of music player

I have tried a lot,but i can't get any solution here , I am frustrating now , please help me thanks in adavance

My code is below

Initiate

MediaPlayer mPlayer = new MediaPlayer();

mPlayer.setWakeMode(_context, PowerManager.PARTIAL_WAKE_LOCK);
mPlayer.setOnPreparedListener(this);
mPlayer.setOnCompletionListener(this);
mPlayer.setOnErrorListener(this);
mPlayer.setOnInfoListener(this);
mPlayer.setOnBufferingUpdateListener(this);

musicseekbar =(SeekBar)findViewById(R.id.seekbar); 
        currenttime = (TextView)findViewById(R.id.runningtime);
        totaltime = (TextView)findViewById(R.id.totaltime);
        setSeekbar();


try {
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
}
mPlayer.setDataSource(manualUrl);
mPlayer.prepareAsync();

Other stuf

       private void setSeekbar() {
        // TODO Auto-generated method stub
        musicseekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

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

            }

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

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub

                if (fromUser) {

                    if (mPlayer != null && mPlayer.isPlaying()) {
                        MusicSeekto(progress,Globals.PlayerDuration);
                        seekBar.setProgress(progress);
                    }
                }
            }
        });
    }

    public void setSeekBarpositionDetail(int position,int currenttime1,int totaltime1) {
        // TODO Auto-generated method stub
        if (musicseekbar != null) {
            musicseekbar.setProgress(currenttime1);
        }
    }


    public void setPlayerInfo(int duration) {

        musicseekbar.setMax(duration); // set maximum here
    }


    public void MusicSeekto(int i, int playerDuration) {
        // TODO Auto-generated method stub

        if (mPlayer != null) {
            mPlayer.seekTo(i);  //problem is here , I get here exact "i", but  seekto(pos) does't start from that position.

        }
    }

        public void primarySeekBarProgressUpdater() {

        if (mPlayer != null) {
            stop = (int) (((float) mPlayer.getCurrentPosition() / Globals.PlayerDuration) * 100);

            setSeekBarpositionDetail(stop, mPlayer.getCurrentPosition(), mPlayer.getDuration());
        }

        if (mPlayer != null) {
            if (mPlayer.isPlaying()) {

                notification = new Runnable() {
                    public void run() {
                        primarySeekBarProgressUpdater();
                    }
                };
                handler.postDelayed(notification, 100);
            }
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
        playerstarting();
        primarySeekBarProgressUpdater();
    }

    private void playerstarting() {
        // TODO Auto-generated method stub

        if (mPlayer != null) {
            mPlayer.start();
        }
        Globals.PlayerDuration = mPlayer.getDuration();
        changingImage(false);
        setPlayerInfo(mPlayer.getDuration());// seekbar value passing from here
    }
Community
  • 1
  • 1
Mayur Raval
  • 3,250
  • 6
  • 34
  • 57

1 Answers1

0

MediaPlayer.seekto doesn't take percentage. It takes milliseconds. Yet you're passing in percentage. That isn't going to work. Instead, try passing in

((float)(i*mPlayer.getDuration()))/100.0
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I am not getting percentage there, i have set totaltime to the maximum in seekbar . and there i have get in miliseconds , and that miliseconds are seekto(pos) in the music player. i am still getting problem. – Mayur Raval Jan 13 '15 at 08:32
  • You haven't in the code you posted here. You may want to post the entire code and clean up the commented out sections so we can figure out what's actually going on. – Gabe Sechan Jan 13 '15 at 08:37
  • I had refectored code and also commented that line which setMax to the seekbar ,Please check, If any any further information need then tell me. – Mayur Raval Jan 13 '15 at 09:02