void MainWindow::on_pushOpenFolder_clicked() { m_notUsed.clear(); QString dir = QFileDialog::getExistingDirectory(this, tr("Open MP3 Source Directory"), pSet->value("src_dir").toString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); if (!dir.isEmpty()) { pSet->setValue("src_dir", dir); ui->lineSrcFolder->setText(dir); cleanTags(); QDir mp3Dir(dir, "*.mp3"); QStringList files = mp3Dir.entryList(); TagLib::MPEG::File* ptlFile; foreach (QString file, files) { QString path = QString("%1/%2").arg(mp3Dir.absolutePath()).arg(file); ptlFile = new TagLib::MPEG::File(reinterpret_cast<const wchar_t *>(path.utf16())); if (ptlFile && ptlFile->isOpen()) { tagFiles.append(ptlFile); } else if(ptlFile) { m_notUsed.append(path); delete ptlFile; } }
/*! * \brief Read the albumart image from the file * * \param filename The filename for which we want to find the length. * \param type The type of image we want - front/back etc * \returns A pointer to a QImage owned by the caller or NULL if not found. */ QImage* MetaIOID3::getAlbumArt(QString filename, ImageType type) { QImage *picture = new QImage(); AttachedPictureFrame::Type apicType = AttachedPictureFrame::FrontCover; switch (type) { case IT_UNKNOWN : apicType = AttachedPictureFrame::Other; break; case IT_FRONTCOVER : apicType = AttachedPictureFrame::FrontCover; break; case IT_BACKCOVER : apicType = AttachedPictureFrame::BackCover; break; case IT_CD : apicType = AttachedPictureFrame::Media; break; case IT_INLAY : apicType = AttachedPictureFrame::LeafletPage; break; default: return picture; } QByteArray fname = filename.toLocal8Bit(); TagLib::MPEG::File *mpegfile = new TagLib::MPEG::File(fname.constData()); if (mpegfile) { if (mpegfile->isOpen() && !mpegfile->ID3v2Tag()->frameListMap()["APIC"].isEmpty()) { TagLib::ID3v2::FrameList apicframes = mpegfile->ID3v2Tag()->frameListMap()["APIC"]; for(TagLib::ID3v2::FrameList::Iterator it = apicframes.begin(); it != apicframes.end(); ++it) { AttachedPictureFrame *frame = static_cast<AttachedPictureFrame *>(*it); if (frame && frame->type() == apicType) { picture->loadFromData((const uchar *)frame->picture().data(), frame->picture().size()); return picture; } } } delete mpegfile; } delete picture; return NULL; }
/*! * \brief Open the file to read the tag * * \param filename The filename * \returns A taglib file object for this format */ TagLib::MPEG::File *MetaIOID3::OpenFile(const QString &filename) { QByteArray fname = filename.toLocal8Bit(); TagLib::MPEG::File *mpegfile = new TagLib::MPEG::File(fname.constData()); if (!mpegfile->isOpen()) { delete mpegfile; mpegfile = NULL; } return mpegfile; }
void LocalSong::setInfo(const QString &filePath) { QFileInfo fileInfo(filePath); fileExtension = fileInfo.completeSuffix(); QString singer=""; QString sname=""; QString album=""; QImage covpic; if(fileExtension == "m4a") { TagLib::MP4::File *mp4File = new TagLib::MP4::File( QFile::encodeName(filePath).constData()); if(false==mp4File->isOpen()) return; sname=QString(mp4File->tag()->title().toCString(true)); singer=QString(mp4File->tag()->artist().toCString(true)); album=QString(mp4File->tag()->album().toCString(true)); TagLib::MP4::ItemListMap itemListMap = mp4File->tag()->itemListMap(); TagLib::MP4::Item albumArtItem = itemListMap["covr"]; TagLib::MP4::CoverArtList albumArtList = albumArtItem.toCoverArtList(); TagLib::MP4::CoverArt albumArt = albumArtList.front(); QImage cover; cover.loadFromData((const uchar *) albumArt.data().data(), albumArt.data().size()); covpic = cover.scaled( 50, 50, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); if(!covpic.isNull()) { qDebug()<<"读取M4A封面信息成功"; }else { covpic.load(":/image/image/playIcon.png"); qDebug()<<"读取音乐封面信息失败"; } } else if(fileExtension == "mp3") { TagLib::MPEG::File *mpegFile = new TagLib::MPEG::File( QFile::encodeName(filePath).constData()); if(false==mpegFile->isOpen()) return; else { sname=QString(mpegFile->tag()->title().toCString(true)); singer=QString(mpegFile->tag()->artist().toCString(true)); album=QString(mpegFile->tag()->album().toCString(true)); auto tag = mpegFile->ID3v2Tag(false); auto list = tag->frameListMap()["APIC"]; if(!list.isEmpty()) { auto frame = list.front(); auto pic = reinterpret_cast<TagLib::ID3v2::AttachedPictureFrame *>(frame); if(pic && !pic->picture().isNull()) { QImage cover; if(cover.loadFromData(QByteArray::fromRawData(pic->picture().data(), pic->picture().size()))) { covpic = cover.scaled( 50, 50, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); qDebug()<<"读取MP3封面信息成功"; } } } else { covpic.load(":/image/image/playIcon.png"); qDebug()<<"读取音乐封面信息失败"; } } } singerlist.append(singer); snamelist.append(sname); albumlist.append(album); covPiclist.append(covpic); int covCount=covPiclist.length(); QString covpicpath=QDir::tempPath()+QString("/%1.png").arg(covCount); covpic.save(covpicpath); QUrl covpicUrl=QUrl::fromLocalFile(covpicpath); qDebug()<<"歌名为"<<sname; qDebug()<<"歌手为"<<singer; qDebug()<<"专辑为"<<album; qDebug()<<"URL为"<<covpicUrl; emit setSongInfo(singer,sname,album,covpicUrl); return; }