PDA

View Full Version : Retrive audio duration



Imhotep
31st July 2016, 15:50
Is there a way to determine the length of an audio file? The QMediaPlayer duration (http://doc.qt.io/qt-5/qmediaplayer.html#duration-prop) prop only works for the current played file. So, searching the web, I thought that a solution could be QAudioBuffer class usage.
So, this is my attempt



void MainWindow::openmediaOnClick()
{
*filenames = QFileDialog::getOpenFileNames(this, tr("Open Media"), "D:", tr("Audio Files (*.mp3 *.wav *.ogg)"));
playlist->clear();
tracklist->setRowCount(filenames->size());
for (int i=0; i < filenames->size(); i++) {
playlist->addMedia(QUrl(filenames->at(i)));
tracklist->setItem(i, 0, new QTableWidgetItem(QString::number(i+1)));
tracklist->setItem(i, 1, new QTableWidgetItem(filenames->at(i)));
QAudioBuffer *audioBuffer(QFile(filenames->at(i)));
qDebug() << audioBuffer->duration();

};
this->runPlaylist();
}


Qt send me this error message:
error: cannot convert 'QFile' to 'QAudioBuffer*' in initialization
QAudioBuffer *audioBuffer(QFile(filenames->at(i)));

Can you help me?

anda_skoa
31st July 2016, 16:00
As the error says, there is no way for the compiler to convert a QFile object into a QAudioBuffer object.

None of the four constructors of QAudioBuffer takes a QFile by value or reference.

In any case, isn't it more likely that you need QAudioDecoder?

Cheers,
_

Imhotep
31st July 2016, 17:18
I should receive information about the audio file in the queue, and therefore have not yet been processed. Using



decoder->setSourceFilename(filenames->at(i));
qDebug() << decoder->duration();


Qt returns -1 for all files

anda_skoa
31st July 2016, 17:39
Maybe you need to call start() on it.

Cheers,
_

Imhotep
31st July 2016, 17:59
Even calling start(), the result does not change



decoder->setSourceFilename(filenames->at(i));
decoder->start();
qDebug() << decoder->duration();


This is the Qt error message on debug:
defaultServiceProvider::requestService(): no service found for - "org.qt-project.qt.audiodecode"

anda_skoa
1st August 2016, 00:33
Hmm.

Maybe with a QMediaPlayer using a QMetaContext created from a QUrl that was created from the filename with ::fromLocalFile()?

Cheers,
_

Imhotep
1st August 2016, 23:48
Hmm.

using a QMetaContext

Cheers,
_

Link broken :rolleyes:

anda_skoa
2nd August 2016, 00:53
It's because it is a QMediaContent.

You could have easily guessed that by looking at the documentation of the first class.

And the last link is QUrl::fromLocalFile() before you complain again after not looking yourself.

Cheers,
_