Question

I'm developing a video player using Qmediaplayer. when I set a label to show duration of a video it display wrong value.

float duration = mediaPlayer.duration() / 1000.0f;
ui->lblDuration->setText(QDateTime::fromTime_t(duration).toString("hh:mm:ss"));

If I play a video where duration is 7 minutes 24 seconds label shows "05:37:24" There is 5 hours and 30 minutes are added to the label and I can't find a reason. Please help me on this guys...

Was it helpful?

Solution

Well I found another way to do this. Little bit long but works perfect.

qint64 duration = mediaPlayer.duration();

int seconds = (duration/1000) % 60;
int minutes = (duration/60000) % 60;
int hours = (duration/3600000) % 24;

QTime time(hours, minutes,seconds);

ui->lblDuration->setText(time.toString());

Here I have used to qint64 as variable type so I don't need to convert float to integer (I have used float value in my question). I have used remainders values to set seconds,minutes and hours. Example: in hours, I have devided duration in 3600000. Which means hours(60) * minutes(60) * milliseconds(1000). Which gives me exact hours and then get the remainder of 24(days) because to make sure there are not days. (which is really not possible for a video but to make sure).

Then converting them to Qtime (because I need this in "hh:mm:ss" format). Later convert to string and displayed using a label.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top