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.
Asked
Active
Viewed 4,002 times
3

László Papp
- 51,870
- 39
- 111
- 135

Donotalo
- 12,748
- 25
- 83
- 121
-
see if this helps.. http://stackoverflow.com/questions/13561791/how-to-get-duration-of-audio-and-video-files-in-qt-without-using-qmediaplayer – Digital_Reality Dec 29 '13 at 17:44
-
or this: http://qt-project.org/doc/qt-5.0/qtmultimedia/qmediaplayer.html#duration-prop – leemes Dec 29 '13 at 17:44
-
@Digital_Reality: that does not provide a qt way of doing it (phonon or taglib is not part of Qt). Also, the links seems to be broken over there. Someone would need to fix it, I think. – László Papp Dec 29 '13 at 17:45
-
@LaszloPapp I think both ways are mentioned there.. but seems links are broken. Never mind. – Digital_Reality Dec 29 '13 at 17:47
-
@LaszloPapp: I need to try QAudioBuffer's duration property once I get back to home. I don't like to use QMediaPlayer's duration as this can change. I need to list song names along with their length so QMediaPlayer's duration doesn't seem a proper way to do that. – Donotalo Dec 30 '13 at 07:42
2 Answers
4
You seem to be looking for the duration
property.
This would be the QML solution:
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().

Community
- 1
- 1

László Papp
- 51,870
- 39
- 111
- 135
-
-
@Donotalo: have you tried QAudioBuffer audioBuffer(myFile.readAll()...);? – László Papp Dec 31 '13 at 00:21
-
Can you please provide a working code? QAudioBuffer constructor seems to require QAudioFormat. – Donotalo Dec 31 '13 at 02:20
-
@Donotalo: try http://qt-project.org/doc/qt-5/qaudiodeviceinfo.html#preferredFormat – László Papp Dec 31 '13 at 02:22
-
Using `QAudioBuffer::duration()` to get the length of a song appears to be too heavy. First, I need to detect the format of the audio. Given a random file, I don't know how to detect the format of the audio using Qt. I believe your solution will be applicable while recording audio - in that case I'll know the format and relevant settings. – Donotalo Jan 02 '14 at 07:39
0
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.

Angie Quijano
- 4,167
- 3
- 25
- 30
-
That is not good enough. As per Qt documentation on QMediaPlayer::setMedia: Note: This function returns immediately after recording the specified source of the media. It does not wait for the media to finish loading and does not check for errors. Listen for the mediaStatusChanged() and error() signals to be notified when the media is loaded and when an error occurs during loading. So you would have to setup an event loop and wait. – Danol Apr 23 '20 at 17:10