以时间格式从videoview获取当前位置

问题描述 投票:0回答:2

我正在编写一个应用程序,使用FFMPEG修剪android中的视频。我需要像FFMPEG那样以时间单位给00:00:10赋值。目前我有一个videoview和两个buttons。第一个按钮是startTime,第二个按钮是endTime。当点击按钮时,我使用getCurrentPosition来获取视频的当前位置。我也有一个MediaController附加到我的videoview

现在我面临的问题是,我得到当前位置的int值,我无法传递给FFMPEG,我如何准确地将其转换为以时间单位获取值,以便我可以将其传递给FFMPEG来修剪视频。我已经给出了以下代码供您参考。有没有其他方法来获得除此之外的当前时间。提前致谢。

        tVideoView = (VideoView) findViewById(R.id.tVideoView);
        startBtn = (Button) findViewById(R.id.tStartBtn);
        endBtn = (Button) findViewById(R.id.tEndBtn);
        trimBtn = (Button) findViewById(R.id.trimBtn);

        tVideoView.setVideoPath(videoPath);
        duration = tVideoView.getDuration();
        mMedia = new MediaController(this);

        mMedia.setMediaPlayer(tVideoView);
        mMedia.setAnchorView(tVideoView);
        tVideoView.setMediaController(mMedia);

        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTime = tVideoView.getCurrentPosition();
                startBtn.setText(String.valueOf(startTime));
            }
        });

        endBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                endTime = tVideoView.getCurrentPosition();
                endBtn.setText(String.valueOf(endTime));
            }
        });
android video ffmpeg
2个回答
1
投票

将毫秒转换为时间格式的功能

String getVideoTime(long ms)
{
    ms/=1000;
    return (ms/3600)+":"+((ms%3600)/60)+":"+((ms%3600)%60);
}

并在您的runnable或计时器中调用它来更新UI

String tm = getVideoTime(
    videoView.getCurrentPosition()) + 
    " / " +
    getVideoTimeFormat(videoView.getCurrentPosition()
);
tvVideoTime.setText(tm);

0
投票

您可以使用简单方法以Date格式获取毫秒时间,

/**
 * Return date in specified format.
 * @param milliSeconds Date in milliseconds
 * @param dateFormat Date format 
 * @return String representing date in specified format
 */
public static String getDate(long milliSeconds, String dateFormat)
{
    // Create a DateFormatter object for displaying date in specified format.
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);

    // Create a calendar object that will convert the time value in milliseconds to date. 
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(milliSeconds);
     return formatter.format(calendar.getTime());
}

要调用该方法,

String time = (getDate(12184, "hh:mm:ss"));

这将返回标准时间格式,如06:00:12

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