每次拖动时,媒体播放器搜索栏从起始位置开始播放

时间:2016-02-09 07:01:28

标签: android seekbar

我正在尝试使用搜索栏监听器远程播放MP3歌曲。

但是当我尝试拖动进度条时,它会从歌曲的开头播放。它应该从那个拖累的位置发挥作用。

我的代码在这里: -

try{
        mediaplayer.setDataSource("http://yyy.com/yyy/wp-content/uploads/2016/01/Sleep-Sanctuary.mp3");
        mediaplayer.prepareAsync(); 
        int min = 29;
        int sec = 41;

        int minc = min * 60000;
        int secc = sec * 1000;

        time = minc + secc;

        mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
            int seconds = (int) (time / 1000) % 60;
            int minutes = (int) ((time / (1000 * 60)) % 60);
            s_time.setText("" + minutes + ":" + seconds);
            seekbar.setMax(time);
            seekbar.postDelayed(onEverySecond, 1000);
            mediaplayer.seekTo(position);
            if (position == 0) {

                mediaplayer.start();
            } else {
                //if we come from a resumed activity, video playback will be paused
                mediaplayer.pause();
            }
            }
        });
        mediaplayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            public void onBufferingUpdate(MediaPlayer mp, int percent) {

                seekbar.setSecondaryProgress(percent);                  
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

            }
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {
            if (fromUser) {
                // this is when actually seekbar has been seeked to a new position
                mediaplayer.seekTo(progress);
            }
        }
    });

onEverySecond = new Runnable(){

        @Override
        public void run() {

            if (seekbar != null) {
                seekbar.setProgress(mediaplayer.getCurrentPosition());

            }

                if (mediaplayer.isPlaying()) {
                seekbar.postDelayed(onEverySecond, 1000);
            }

            int startTime = mediaplayer.getCurrentPosition();

            int seconds = (int) (startTime / 1000) % 60;
            int minutes = (int) ((startTime / (1000 * 60)) % 60);
            f_time.setText("" + minutes + ":" + seconds);

        }
    };

0 个答案:

没有答案