How to get length of a song using Qt 5.1?

感情迁移 提交于 2019-12-24 00:52:33

问题


How can I get the length of a song in Qt 5.1 API? I think I can read it using Length metadata but I'm not sure if this metadata will be available for all audio files.


回答1:


You seem to be looking for the duration property.

This would be the QML solution:

duration : int

This property holds the duration of the media in milliseconds.

If the media doesn't have a fixed duration (a live stream for example) this will be 0.

This would be the C++ solution:

qint64 QAudioBuffer::duration() const

Returns the duration of audio in this buffer, in microseconds.

This depends on the /l format(), and the frameCount().




回答2:


The other way you can achieve this is using QMediaPlayer. An example would be:

QMediaPlayer *player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile("C:/Users/Music/mySong.mp4"));
qDebug()<<"Song duration: "<<player->duration();

I hope this can help.



来源:https://stackoverflow.com/questions/20828151/how-to-get-length-of-a-song-using-qt-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!