qt for android 之实现后台扫描音乐文件

手机多媒体开发的时候经常需要后台扫描音乐文件,本篇使用qt一些库实现简单的后台扫描的类,继承了Qthread
playlist.h

#ifndef PLAYLIST_H
#define PLAYLIST_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class PlayList : public QThread
{
    Q_OBJECT
public:
    explicit PlayList(QObject *parent = 0);
    ~PlayList();

    Q_PROPERTY(QJsonArray playlist READ playlist NOTIFY playlistChanged)
    QJsonArray playlist();

    Q_INVOKABLE void findMusic();

    Q_INVOKABLE int getItemsLength(){
        return musicPath.count();
    }
    Q_INVOKABLE QString getMusicName(int id){
        return musicPath.at(id).fileName();
    }
    Q_INVOKABLE QString getMusicPath(int id){
        return musicPath.at(id).filePath();
    }
    void run();



signals:
    void playlistChanged();

public slots:
private:
    QList musicPath;

};

#endif // PLAYLIST_H

playlist.cpp

#include "playlist.h"

PlayList::PlayList(QObject *parent) : QThread(parent)
{

}

PlayList::~PlayList()
{

}
void PlayList::findMusic(){
    this->start();
}

QJsonArray PlayList::playlist(){
    QJsonArray jsarr;
    QMutex mutex;
    mutex.lock();
    for(auto it:musicPath){
        QJsonObject obj;
        obj.insert("name",it.fileName());
        obj.insert("path","file://"+it.filePath());
        jsarr.append(obj);
    }
    mutex.unlock();
    return jsarr;
}
void PlayList::run(){
    QMutex mutex;
    mutex.lock();
    QString path="/mnt/sdcard";

    //get all files's filters
    QStringList filters;
    filters<

效果图:

qt for android 之实现后台扫描音乐文件_第1张图片

你可能感兴趣的:(c++,qt5)