使用onStopTrackingTouch将视频视图与SeekBar同步

问题描述 投票:3回答:4

我正在制作自定义视频播放器,已成功将视频进度同步到seekbak。我希望当用户移动搜索栏处理程序时,视频视图应将其进度与搜索栏的进度同步。我也做到了这一点,但它的表现也不尽如人意。让我详细说明这个问题,当我触摸搜索栏处理程序并将其(向前或向后)移动时,我会更新搜索栏进度,但是当我离开搜索栏时,搜索栏处理程序会向前/向后前进一步,然后开始播放而不是从开始播放我离开的同一点。

这是我的代码:

public class MainActivity extends Activity implements OnSeekBarChangeListener {

    private VideoView vvVideo;
    private SeekBar sbVideo;
    private Handler mHandler = new Handler();
    private Utilities utils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_video_player);
        initOnjects();
        playVideo();
    }

    private void initOnjects() {
        vvVideo = (VideoView) findViewById(R.id.vvVideo);
        sbVideo = (SeekBar) findViewById(R.id.sbVideo);
        sbVideo.setOnSeekBarChangeListener(this);
        utils = new Utilities();
    }

    private void playVideo() {
        Uri uri = Uri
                .parse("android.resource://com.sunshine.handsonequation/raw/title_video");
        vvVideo.setVideoURI(uri);
        sbVideo.setProgress(0);
        sbVideo.setMax(100);
        vvVideo.start();
        // Updating progress bar
        updateProgressBar();
    }

    private void updateProgressBar() {
        mHandler.postDelayed(updateTimeTask, 100);
    }

    private Runnable updateTimeTask = new Runnable() {

        @Override
        public void run() {
            long totalDuration = vvVideo.getDuration();
            long currentDuration = vvVideo.getCurrentPosition();

            // update progress bar
            int progress = (int) (utils.getProgressPercentage(currentDuration,
                    totalDuration));
            sbVideo.setProgress(progress);
            mHandler.postDelayed(this, 100);

        }
    };

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

    }

    // when user starts moving the progress handler
    @Override
    public void onStartTrackingTouch(SeekBar seekbar) {
        // remove message Handler from updating progress bar
        mHandler.removeCallbacks(updateTimeTask);
    }

    // when user stops moving the progress handler
    @Override
    public void onStopTrackingTouch(SeekBar seekbar) {
        mHandler.removeCallbacks(updateTimeTask);
        int totalDuration = vvVideo.getDuration();
        int currentPosition = utils.progressToTimer(sbVideo.getProgress(),
                totalDuration);

        // forward or backward to certain seconds
        vvVideo.seekTo(currentPosition);

        // update timer progress again
        updateProgressBar();
    }
}

Utility Class:

public class Utilities {

    /**
     * Function to convert milliseconds time to
     * Timer Format
     * Hours:Minutes:Seconds
     * */
    public String milliSecondsToTimer(long milliseconds){
        String finalTimerString = "";
        String secondsString = "";

        // Convert total duration into time
           int hours = (int)( milliseconds / (1000*60*60));
           int minutes = (int)(milliseconds % (1000*60*60)) / (1000*60);
           int seconds = (int) ((milliseconds % (1000*60*60)) % (1000*60) / 1000);
           // Add hours if there
           if(hours > 0){
               finalTimerString = hours + ":";
           }

           // Prepending 0 to seconds if it is one digit
           if(seconds < 10){
               secondsString = "0" + seconds;
           }else{
               secondsString = "" + seconds;}

           finalTimerString = finalTimerString + minutes + ":" + secondsString;

        // return timer string
        return finalTimerString;
    }

    /**
     * Function to get Progress percentage
     * @param currentDuration
     * @param totalDuration
     * */
    public int getProgressPercentage(long currentDuration, long totalDuration){
        Double percentage = (double) 0;

        long currentSeconds = (int) (currentDuration / 1000);
        long totalSeconds = (int) (totalDuration / 1000);

        // calculating percentage
        percentage =(((double)currentSeconds)/totalSeconds)*100;

        // return percentage
        return percentage.intValue();
    }

    /**
     * Function to change progress to timer
     * @param progress -
     * @param totalDuration
     * returns current duration in milliseconds
     * */
    public int progressToTimer(int progress, int totalDuration) {
        int currentDuration = 0;
        totalDuration = (int) (totalDuration / 1000);
        currentDuration = (int) ((((double)progress) / 100) * totalDuration);

        // return current duration in milliseconds
        return currentDuration * 1000;
    }
}

我不知道为什么会这样,希望你们能给我一些提示或解决方案。

android android-mediaplayer android-videoview seekbar
4个回答
13
投票

我认为您对该实用程序类的使用使自己过于复杂。将搜寻栏的最大进度设置为等于视频时长。然后,在seekbar侦听器上,您可以直接获取它的进度并进行搜索。希望您能理解我键入的波纹管,这是我的第一个答案:)。

private void playVideo() {
    Uri uri = Uri..parse("android.resource://com.sunshine.handsonequation/raw/title_video");
    vvVideo.setVideoURI(uri);
    vvVideo.start();
    // Updating progress bar
    updateProgressBar();
}

private void updateProgressBar() {
    mHandler.postDelayed(updateTimeTask, 100);
}

private Runnable updateTimeTask = new Runnable() {
    public void run() {
        sbVideo.setProgress(vvVideo.getCurrentPosition());
        sbVideo.setMax(vvVideo.getDuration());
        mHandler.postDelayed(this, 100);
    }
};

public void onProgressChanged(SeekBar seekbar, int progress,boolean fromTouch) {

}
public void onStartTrackingTouch(SeekBar seekbar) {
    mHandler.removeCallbacks(updateTimeTask);
}
public void onStopTrackingTouch(SeekBar seekbar) {
    mHandler.removeCallbacks(updateTimeTask);
    vvVideo.seekTo(sbVideo.getProgress());
    updateProgressBar();
}

0
投票

SeekBar功能中将视频搜索到onProgressChanged进度。

@Override
    public void onProgressChanged(SeekBar seekbar, int progress,
            boolean fromTouch) {
    if(fromTouch)
    {
        vvVideo.seekTo(progress);
    }

    }

0
投票

不正确的答案应该起作用,而不是

vvVideo.seekTo(progress); 

应该是

vvVideo.seekTo(progress*1000);

seekTo时间应该以毫秒为单位

http://developer.android.com/reference/android/widget/VideoView.html#seekTo(int)


0
投票

int mSeekValue =(进度* mTotalDurationInMilliSeconds)/ 1000;vvVideo.seekTo(mSeekValue);

而不是

vvVideo.seekTo(progress * 1000);

© www.soinside.com 2019 - 2024. All rights reserved.