简体   繁体   English

QMediaplayer持续时间错误

[英]QMediaplayer duration is wrong

I'm developing a video player using Qmediaplayer. 我正在使用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. 如果我播放持续时间为7分24秒的视频标签显示“05:37:24”标签上添加了5小时30分钟,我找不到原因。 Please help me on this guys... 请帮帮我这个家伙......

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). 这里我习惯将qint64作为变量类型,所以我不需要将float转换为整数(我在我的问题中使用了浮点值)。 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). 示例:以小时为单位,我将持续时间分配为3600000.这意味着小时(60)*分钟(60)*毫秒(1000)。 Which gives me exact hours and then get the remainder of 24(days) because to make sure there are not days. 这给了我确切的时间,然后得到24(天)的剩余时间,因为要确保没有几天。 (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). 然后将它们转换为Qtime(因为我需要以“hh:mm:ss”格式)。 Later convert to string and displayed using a label. 稍后转换为字符串并使用标签显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM