问题
I have a VideoView for Play Video and MediaControler for it
I want Display time of video in textview and change it per time of video
I use this code for Playing video :
private void play(String uri) {
_player.setVisibility(View.VISIBLE);
_player.setVideoURI(Uri.parse(uri));
MediaController mc = new MediaController(this);
_player.setMediaController(mc);
_player.start();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
_player.setLayoutParams(new FrameLayout.LayoutParams(width, width));
}
回答1:
this method used for convert msec to time and _player.getDuration() is used on Prepare.
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;
}
回答2:
you can use the
MediaPlayer.getDuration()
to retrieve the total time of the video. You'll better set a seekbar to retrieve the progress of the video and write it to the textview or better on a chronometer. Or you can simply start the chronometer when the video starts.
回答3:
You have to add a seekbar in the layout xml file, than implement the seekbar , to get real-time progress of the video playing use
MediaPlayer.getCurrentPosition()
You have to write a runnable method to update the seekbar every second with this method:
myseekbar.setProgress(myMediaPlayer.getCurrentPosition())
you have to retrieve the time from getCurrentPosition.
Accept my answer?
回答4:
Here is another conversion metod using "TimeUnit" :
static String getTimeString(Long millis) {
return String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
}
来源:https://stackoverflow.com/questions/16130324/how-to-display-time-of-videoview-in-android