Extract media duration from metadata
-
-
Hi!
QAudioDecoder
can do this for you.void MainWindow::on_pushButton_clicked() { QAudioDecoder *ad = new QAudioDecoder(this); ad->setSourceFilename("/home/user/file.mp3"); connect(ad, &QAudioDecoder::durationChanged, this, &MainWindow::onDurationChanged); ad->start(); } void MainWindow::onDurationChanged(qint64 d) { QTime t(0,0,0,0); t = t.addMSecs(d); ui->label->setText( t.toString("hh:mm:ss") ); }
Edit: Removed code that deleted
ad
to early. -
Depending on your platform, audio decoding might not be provided by the backend. See: http://wiki.qt.io/Qt_5.7_Multimedia_Backends#Audio_plugins
-
void MainWindow::playerOnMediaStatusChanged(QMediaPlayer::MediaStatus status) { if (status == QMediaPlayer::BufferedMedia) { QVariant duration = player->metaData("Duration"); qDebug() << duration.toLongLong(); } }
returns 0
Yes, I know, there is player->duration(), but as I mentioned early works only for the current media
-
Yeah, you're right.
However, it is possible resolved by using a few tricks.
like this:auto playlist = new QMediaPlaylist; playlist->addMedia(QUrl::fromLocalFile("/Sample/Music/1.mp3")); playlist->addMedia(QUrl::fromLocalFile("/Sample/Music/2.mp3")); playlist->addMedia(QUrl::fromLocalFile("/Sample/Music/3.mp3")); player->setPlaylist(playlist); playlist->setCurrentIndex(0); //player->play(); static bool isExtractMode = true; connect(player, &QMediaPlayer::mediaStatusChanged, [&, playlist](QMediaPlayer::MediaStatus status){ if (isExtractMode) { if (status == QMediaPlayer::LoadedMedia) { qDebug() << "Duration: " << player->metaData("Duration"); int index = playlist->nextIndex(); if (index < 0) { isExtractMode = false; playlist->setCurrentIndex(0); } else playlist->setCurrentIndex(index); } } });
-
[SOLVED]
I had to resort to the help of TagLibs library. And since the documentation available how to build this library to make it interact with Qt are very confusing and outdated, I proceeded to make available the library that I managed, with great difficulty, to compile by my hand:
http://www.unitscan.org/archives/301
I hope will be usefull