Search code examples
qtqt5meta-tagsqmediaplayer

Qt Multimedia - how to force read tags from media file


It seems to be QMediaPlayer doesn't read tags of playlist files without of executing method play() of current track. Is there a way to force read tags from QMediaPlayer playlist without of playing it?

Sorry if this if a noob question, but I spent a lot of time for searches.

Thanks


Solution

  • Don't waste your time to use QMediaPlayer to read tags (this solution works good, but difficult for many files in list), simply use TagLib or another open-source library.

    For example, 3 simple steps to use TagLib in Qt:

    1.Compile taglib from source:

    $ pwd
    /home/user/taglib-1.9.1
    $ cmake .
    $ make
    

    It's enough to deploy working instance of tag library. Really :)

    2.Include headers and library in your project, simply add this or your custom path to project file:

    unix:!macx: LIBS += -L$$PWD/3rdparty/taglib-1.9.1/taglib/ -ltag
    INCLUDEPATH += $$PWD/3rdparty/taglib-1.9.1/taglib/Headers
    DEPENDPATH += $$PWD/3rdparty/taglib-1.9.1/taglib/Headers
    

    3.Use it, very simple function to get media tags from files, in this example artist and title of track:

        #include <fileref.h>
        #include <tag.h>
        QString gettags(QString mediafile){
            QString string;
            TagLib::FileRef file(mediafile.toUtf8());
            TagLib::String artist_string = file.tag()->artist();
            TagLib::String title_string = file.tag()->title();
            QString artist = QString::fromStdWString(artist_string.toWString());
            QString title = QString::fromStdWString(title_string.toWString());
            string = artist + " - " + title;
            return string;
        }