Results 1 to 8 of 8

Thread: Retrive audio duration

  1. #1
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Retrive audio duration

    Is there a way to determine the length of an audio file? The QMediaPlayer duration 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

    Qt Code:
    1. void MainWindow::openmediaOnClick()
    2. {
    3. *filenames = QFileDialog::getOpenFileNames(this, tr("Open Media"), "D:", tr("Audio Files (*.mp3 *.wav *.ogg)"));
    4. playlist->clear();
    5. tracklist->setRowCount(filenames->size());
    6. for (int i=0; i < filenames->size(); i++) {
    7. playlist->addMedia(QUrl(filenames->at(i)));
    8. tracklist->setItem(i, 0, new QTableWidgetItem(QString::number(i+1)));
    9. tracklist->setItem(i, 1, new QTableWidgetItem(filenames->at(i)));
    10. QAudioBuffer *audioBuffer(QFile(filenames->at(i)));
    11. qDebug() << audioBuffer->duration();
    12.  
    13. };
    14. this->runPlaylist();
    15. }
    To copy to clipboard, switch view to plain text mode 

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


    Can you help me?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Retrive audio duration

    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,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Re: Retrive audio duration

    I should receive information about the audio file in the queue, and therefore have not yet been processed. Using

    Qt Code:
    1. decoder->setSourceFilename(filenames->at(i));
    2. qDebug() << decoder->duration();
    To copy to clipboard, switch view to plain text mode 

    Qt returns -1 for all files

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Retrive audio duration

    Maybe you need to call start() on it.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Re: Retrive audio duration

    Even calling start(), the result does not change

    Qt Code:
    1. decoder->setSourceFilename(filenames->at(i));
    2. decoder->start();
    3. qDebug() << decoder->duration();
    To copy to clipboard, switch view to plain text mode 

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

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Retrive audio duration

    Hmm.

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

    Cheers,
    _

  7. #7
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Re: Retrive audio duration

    Quote Originally Posted by anda_skoa View Post
    Hmm.

    using a QMetaContext
    Cheers,
    _
    Link broken

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Retrive audio duration

    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,
    _

Similar Threads

  1. How to Retrive data...?
    By gunturrohith in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 25th April 2015, 09:10
  2. Replies: 1
    Last Post: 26th November 2012, 19:09
  3. Retrive values using qt
    By jeyanthinatarajan in forum Newbie
    Replies: 1
    Last Post: 5th July 2012, 08:31
  4. How to get video file duration in Qt?
    By newtolinux in forum Qt Programming
    Replies: 2
    Last Post: 26th November 2010, 11:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.