Qt 使用tablib获取多媒体tag信息

最近项目需要, 要获取音乐文件tag信息. 有两个方式, 本人偏向第二种方式. 效率比较高,可控性比较好.

一.QML方式

使用QML Audio component 进行解析. 将多媒体文件都放到Playlist中, 在遍历每一个文件取出文件中的tag.

import QtQuick 2.7
import QtQuick.Controls 2.4
import QtMultimedia 5.8

Item {
    property alias playList: parseMultimedia.playlist
    property alias musicPlayer: musicPlayer

    WorkerScript {
        id: worker
        property bool running: false
        property var type
        source: "dataloader.js"
        onMessage: {
            if (messageObject.action === 'sortArtist') {
                var msg = {'action': 'sortAlbum', 'model': songsListModel,
                    'dest': albumListModel};
                worker.sendMessage(msg);
            }
        }
    }

    Audio {
        id: musicPlayer
        autoPlay: true
        autoLoad: true
        onSourceChanged: {
        }
        playlist: Playlist {
            id: musicPlayList
        }
    }

    Audio {
        id: parseMultimedia
        property var fileName
        //autoPlay :true
        autoLoad: true
        muted: true
        loops: Audio.Infinite
        playlist: parsePlaylist
    }

    Playlist {
        id: parsePlaylist
    }

    Timer {
        id: insertTimer
        property int indexPath: 0
        interval: 200;
        running: false; repeat: true
        onTriggered: {
            if ( indexPath < musicManager.count()) {
                parseMultimedia.play()
                parseMultimedia.pause()
                var title = parseMultimedia.metaData.title

                var artist = parseMultimedia.metaData.albumArtist
                if (artist === undefined) {
                    artist = 'unknown'
                }

                var album = parseMultimedia.metaData.albumTitle
                if (album === undefined) {
                    album = 'unknown'
                }

                var source = Qt.resolvedUrl(parseMultimedia.playlist.itemSource(indexPath))
                var fileName = musicManager.getFileInfoName(indexPath)
                if (title === undefined) {
                    title =  fileName
                }
                songsListModel.set(parsePlaylist.currentIndex, {"playlistIndex": parsePlaylist.currentIndex,
                                       "title": parseMultimedia.metaData.title,
                                       "album": album,
                                       "artist": artist,
                                       "source": source,
                                       "fileName": fileName });
                indexPath = indexPath + 1
                parsePlaylist.next()
               // listView.forceLayout();

            } else {
                if (indexPath >= musicManager.count() ) {
                    insertTimer.stop()
                    insertTimer.indexPath = 0
                    appWindow.isReady = true
                    allSongListModelReady();
                  }
            }
        }

        onRunningChanged: {
            if (!running) {
                var msg = {'action': 'sortArtist', 'model': songsListModel,
                    'dest':artistListModel};
                worker.sendMessage(msg);
            }
        }
    }

    Timer {
        id: loadModelTimer
        property int indexPath: 0
        running: false; repeat: false
        onTriggered: {
            parsePlaylist.currentIndex = 0
            insertTimer.start()
        }
    }

    Connections {
        target: musicManager
        onLoadfinish: {
            parsePlaylist.clear()

            loadModelTimer.interval = musicManager.count()
            loadModelTimer.start()
            for (var i = 0; i< musicManager.count(); i++) {
                parsePlaylist.addItem("file://" + musicManager.getFileInfoPath(i))
            }
            loadFolderData();
        }
    }

    function loadFolderData() {
       for (var i = 0; i < musicManager.folderCount(); i++) {

           var item = musicManager.folderItem(i);
           folderListModel.set(i, {"baseName": item.baseName,
                                           "uid": item.uId ,
                                           "dropTarget": "none",
                                           "isFolder": item.isFolder,
                                           "parentFolder": item.parentFolder,
                                           "folderOpen":item.folderOpen,
                                           "source": item.path,
                                           "fileCount": item.fileCount })
       }
    }
    Component.onCompleted: {
        musicManager.runParse();
    }
}

二.C++ tablib 方式

这个没什么好说的, 直接将tablib编译成和一平台一样的类型就可以了.例如: arm. x86.

int main(int argc, char **argv)  
{  
	QString sname;  
	QString singer;  
	QString album;  
	QFileInfo fileInfo(QString("/sdcard/Music/Alone.mp3"));  
	qDebug() << fileInfo.absoluteFilePath();

	const char *fileName = "/sdcard/Music/Alone.mp3";  
	TagLib::MPEG::File *mpegFile = new TagLib::MPEG::File(fileName);  
	qDebug() << "mpegFile: " << mpegFile;  
	if(false == mpegFile->isOpen()) {  
	qDebug() << "-----open failed---";  
}  else  {  
	sname = QString(mpegFile->tag()->title().toCString(true));  
	singer = QString(mpegFile->tag()->artist().toCString(true));  
	album = QString(mpegFile->tag()->album().toCString(true));
	qDebug() << sname;  
	qDebug() << singer;  
	qDebug() << album;  
	}
}

这里有我自己编译QT 项目.
QT tablib

posted @ 2018-09-12 16:44  liu-yonggang  阅读(568)  评论(0编辑  收藏  举报