Seekbar不会从用户拖动它的位置开始播放

时间:2016-07-06 08:26:22

标签: android seekbar

我遇到了seekbar拇指的问题,虽然我已经添加了所有必要的代码以便它工作。仍然,搜索栏拇指没有从用户拖动的点播放。相反,拇指将自己定位回到最后一次播放的位置。任何帮助将不胜感激。

    plus = (ImageButton) findViewById(R.id.imageButton2);
    minus = (ImageButton) findViewById(R.id.imageButton4);


    player = MediaPlayer.create(this, R.raw.songnames);


    player.setLooping(false);
    im = (ImageButton) this.findViewById(R.id.imageButton3);


    seekbar = (SeekBar) findViewById(R.id.mantraProgressBar);
    seekbar.setVisibility(ProgressBar.VISIBLE);
    seekbar.setProgress(0);
    seekbar.setMax(player.getDuration());
    new Thread(this).start();


    im.setOnClickListener(this);
    player.start();
    im.setImageResource(R.drawable.pause_tran);

    Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();


    plus.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int cu = player.getCurrentPosition();
            player.seekTo(cu - 5000);
        }
    });
    minus.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int cu = player.getCurrentPosition();
            player.seekTo(cu + 5000);
        }
    });


}


@Override
public void onClick(View arg0) {
    if (arg0.getId() == R.id.imageButton3) {
        if (player.isPlaying()) {
            player.pause();

            Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
            ImageButton img1 = (ImageButton) this.findViewById(R.id.imageButton3);

            img1.setImageResource(R.drawable.play_tran);

        } else {
            player.start();
            Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();

            ImageButton img1 = (ImageButton) this.findViewById(R.id.imageButton3);
            img1.setImageResource(R.drawable.pause_tran);
        }
    }
}

@Override
public void run() {
    int currentPosition = 0;
    String s;
    int total = player.getDuration();
    while (player != null && currentPosition < total) {
        try {
            Thread.sleep(1000);
            currentPosition = player.getCurrentPosition();
        } catch (InterruptedException e) {
            return;
        } catch (Exception e) {
            return;
        }

        seekbar.setProgress(currentPosition);


    }


}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (fromUser) {
        player.seekTo(progress);
        seekbar.setProgress(progress);
    }
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
}

1 个答案:

答案 0 :(得分:0)

公共类MainActivity扩展了AppCompatActivity {

private MyVideoView mVV;
private Handler mhandler;
private SeekBar seekbar;
private int duration;
private int backPositon = 0;
private int currentPosition;
private SharedPreferences sp;
private MediaController controller;
private TextView tv_curr;
private TextView tv_duration;
private Handler handler;
private boolean mExit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mVV = (MyVideoView) findViewById(R.id.vv);
    seekbar = (SeekBar) findViewById(R.id.seekbar);
    tv_curr = (TextView) findViewById(R.id.tv_currtime);
    tv_duration = (TextView) findViewById(R.id.tv_duration);

    //get duration of video
    getDuration();
    //video start
    playVideo();
    //get current time
    getCurrentDuration();
    //listener
    initEvent();

}

private void initHanlder() {
    handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==0x123){
                tv_curr.setText(timeFormat(mVV.getCurrentPosition()));
                seekbar.setProgress(mVV.getCurrentPosition());
            }

            if(msg.what==0x133){
                int changedProgress = msg.arg1;
                tv_curr.setText(timeFormat(changedProgress));
                mVV.seekTo(changedProgress);
                seekbar.setProgress(changedProgress);
                mVV.start();
            }
        }
    };

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (mVV.getCurrentPosition()<duration){
                Message message = Message.obtain();
                message.what=0x123;
                handler.sendMessage(message);
                SystemClock.sleep(1000);

                if(mExit){
                    break;
                }
            }

        }
    }).start();

}


private void getDuration() {
    mVV.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            duration = mVV.getDuration();
            timeFormat(duration);
            Log.d("TAG", "---duration" + timeFormat(duration));
            seekbar.setMax(duration);
            //TextView  time
            tv_duration.setText(timeFormat(duration));
            //init handler
            initHanlder();
        }
    });
}

private String timeFormat(int time) {   
    StringBuilder mFormatBuilder;
    Formatter mFormatter;
    mFormatBuilder = new StringBuilder();
    mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
    int totalSeconds = time / 1000;

    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;

    mFormatBuilder.setLength(0);
    if (hours > 0) {
        return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
    } else {
        return mFormatter.format("%02d:%02d", minutes, seconds).toString();
    }

}

private void playVideo() {
    File file = new File(Environment.getExternalStorageDirectory(), "cgx_video/db.mp4");
    if (!file.exists()) {
        file.mkdir();
    }
    Log.d("TAG", file.toString());
    mVV.setVideoPath(file.getPath());
    mVV.start();

}


private void initEvent() {
    seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
           currentPosition=progress;


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
             mVV.pause();


        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            if(handler!=null){
                Message message = Message.obtain();
                message.what=0x133;
                message.arg1=currentPosition;
                handler.sendMessage(message);
            }


        }
    });


}


@Override
protected void onPause() {
    super.onPause();
    Log.d("TAG", "---onPause");
    mVV.pause();
    backPositon = mVV.getCurrentPosition();
    Log.d("TAG", "backPositon:" + backPositon);

}

@Override
protected void onStop() {
    super.onStop();
    Log.d("TAG", "---onStop");

}

@Override
protected void onResume() {
    super.onResume();
    Log.d("TAG", backPositon + "---backPositon");
    mVV.seekTo(backPositon);
    mVV.start();
}

@Override
protected void onDestroy() {
    //in case of leak of memeroy
    mExit=true;
    super.onDestroy();

}

}