注意:qt creator用的构建组件是qt 6.2.3 MSVC2019 64bit
- 安装vcpkg
// 我的安装位置C:\vcpkg
git clone https://github.com/microsoft/vcpkg.git C:\vcpkg
cd C:\vcpkg
.\bootstrap-vcpkg.bat// 设置系统环境变量
VCPKG_ROOT = C:/vcpkg
- 用vcpkg安装taglib
vcpkg install taglib
- Qt项目中使用taglib, .pro文件中加入
// 注意vcpkg的安装路径
INCLUDEPATH += C:/vcpkg/installed/x64-windows/include/taglib
LIBS += -LC:/vcpkg/installed/x64-windows/lib -ltag -ltag_c
- Qt项目文件中使用
#include <tag.h>
#include <fileref.h>
...
// 加载音频文件QString filePath = "Music/Tell Me-潘玮柏.mp3";TagLib::FileRef f(filePath.toLocal8Bit().data());// 检查文件是否加载成功if (!f.isNull() && f.tag()) {TagLib::Tag *tag = f.tag();TagLib::AudioProperties *properties = f.audioProperties();// 输出音频文件的标签信息QString title = QString::fromWCharArray(tag->title().toCWString());QString artist = QString::fromWCharArray(tag->artist().toCWString());QString album = QString::fromWCharArray(tag->album().toCWString());QString year = QString::number(tag->year());int duration = properties->length();qDebug() << "Title:" << title;qDebug() << "Artist:" << artist;qDebug() << "Album:" << album;qDebug() << "Duration:" << duration << "seconds";} else {qDebug() << "读取文件失败";}
...