废话不多,直接上代码
widget.cpp文件
#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
#include <QFileInfoList>
#include <qdir>
#include<QFileDialog>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Widget::initWindows();
}
Widget::~Widget()
{
delete ui;
}
void Widget::initWindows()
{
setWindowTitle("Wav 转 bin");
}
void Widget::creatidandsizefile()
{
//ui->outname->addItem("开始制作文件");
QDir *dir=new QDir(qApp->applicationDirPath());
int count = 0;
QString FilePath = qApp->applicationDirPath();
QString creathPath = FilePath + "/wavres.h";
QString creatbinPath = FilePath + "/wav.bin";
QFile::remove(creathPath);//删除文件
QFile::remove(creatbinPath);//删除文件
foreach (QFileInfo mfi, dir->entryInfoList())
{
QString path = qApp->applicationDirPath();
QString RelativeRPath,ResultPath;
int pathSize;
if(mfi.isFile() && mfi.suffix()=="mp3") //判断文件不为空,后缀名为 .mp3 的文件
{
pathSize = path.size();
path = mfi.absoluteFilePath();
RelativeRPath = path.mid(pathSize + 1);
ResultPath = QString("#define %1").arg(RelativeRPath);
ResultPath = ResultPath.left(ResultPath.length() - 4);
ResultPath += " ";
ResultPath += QString("%1").sprintf("0x%02x", count++);
//ui->outname->addItem(path);
//下面开始创建文件写入内容
QFile creathfile(creathPath);
if(!creathfile.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append))
{
ui->outname->addItem("创建.h文件失败");
}
QTextStream in(&creathfile); //写入文件
in<<ResultPath<<"\n";
creathfile.close();
}
}
}
void Widget::on_close_clicked()
{
close();
}
void Widget::on_clear_clicked()
{
ui->outname->clear();
}
void Widget::on_wavcreat_clicked()
{
ui->outname->addItem("开始制作wav bin文件");
QDir *dir=new QDir(qApp->applicationDirPath());
int count = 0;
QString sizedata = "unsigned int data[] = {";
QString FilePath = qApp->applicationDirPath();
QString creathPath = FilePath + "/wavres.h";
QString creatbinPath = FilePath + "/wav.bin";
QFile::remove(creathPath);//删除文件
QFile::remove(creatbinPath);//删除文件
//下面开始创建文件写入内容
QFile creathfile(creathPath);
if(!creathfile.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append))
{
ui->outname->addItem("创建.h文件失败");
}
QTextStream in(&creathfile); //写入文件
in<<"#ifndef __MP3RES_H_"<<"\n";
in<<"#define __MP3RES_H_"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n";
in<<"typedef enum"<<"\n";
in<<"{"<<"\n";
// creathfile.close();
QFile creatbin(creatbinPath);
if(!creatbin.isOpen())
{
if (!creatbin.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append))
{
ui->outname->addItem("创建.bin文件失败");
return ;
}
}
foreach (QFileInfo mfi, dir->entryInfoList())
{
QString path = qApp->applicationDirPath();
QString RelativeRPath,ResultPath;
int pathSize;
if(mfi.isFile() && mfi.suffix()=="wav") //判断文件不为空,后缀名为 .mp3 的文件
{
//count++;
pathSize = path.size();
path = mfi.absoluteFilePath();
RelativeRPath = path.mid(pathSize + 1);
ResultPath = QString(" %1").arg(RelativeRPath);
//ui->outname->addItem(path);
ResultPath = ResultPath.left(ResultPath.length() - 4);
ResultPath += ",";
//ResultPath = ResultPath.left(50);
//ResultPath += QString("%1").sprintf("%d", count);
//QTextStream in(&creathfile); //写入文件
in<<ResultPath<<"\n";
// creathfile.close();
QFile readFile(path);
if(!readFile.isOpen())
{
if (!readFile.open(QIODevice::ReadOnly))
{
ui->outname->addItem("读取文件失败");
return ;
}
}
int fileSize = readFile.size();
uchar *buffer = (uchar *)malloc(fileSize);
memset(buffer,0,fileSize);
readFile.seek(0);
int readfilesize = readFile.read((char *)buffer,fileSize);
qDebug()<<"readfilesize ="<<readfilesize<<fileSize;
ui->outname->addItem(QString("wav文件序号: %1").arg(count)+QString(" 文件大小: %1").arg(readfilesize));
creatbin.write((const char *)&count,4);
creatbin.write((const char *)&readfilesize,4);
count++;
sizedata += QString("%1").sprintf("%d", readfilesize);
sizedata += ", ";
// creatbin.write((char *)buffer,fileSize);
readFile.close();
}
}
creatbin.seek(512);
foreach (QFileInfo mfi, dir->entryInfoList())
{
QString path = qApp->applicationDirPath();
if(mfi.isFile() && mfi.suffix()=="wav") //判断文件不为空,后缀名为 .mp3 的文件
{
path = mfi.absoluteFilePath();
QFile readFile(path);
if(!readFile.isOpen())
{
if (!readFile.open(QIODevice::ReadOnly))
{
ui->outname->addItem("读取文件失败");
return ;
}
}
int fileSize = readFile.size();
uchar *buffer = (uchar *)malloc(fileSize);
memset(buffer,0,fileSize);
readFile.seek(0);
int readfilesize = readFile.read((char *)buffer,fileSize);
qDebug()<<"readfilesize ="<<readfilesize<<fileSize;
creatbin.write((char *)buffer,fileSize);
}
}
creatbin.close();
in<<"}wav_file_id_t;"<<"\n";
sizedata = sizedata.left(sizedata.length() - 2);
in<<sizedata<<"};"<<"\n"<<"\n"<<"\n"<<"\n"<<"#endif "<<"\n";
creathfile.close();
ui->outname->addItem(QString("创建完成,共计wav文件个数: %1").arg(count));
}
void Widget::on_delwav_clicked()
{
QDir *dir=new QDir(qApp->applicationDirPath());
int count = 0;
foreach (QFileInfo mfi, dir->entryInfoList())
{
QString path = qApp->applicationDirPath();
if(mfi.isFile() && mfi.suffix()=="wav") //判断文件不为空,后缀名为 wav的文件
{
count++;
path = mfi.absoluteFilePath();
QFile::remove(path);//删除文件
}
}
ui->outname->addItem(QString("删除wav文件个数: %1").arg(count));
}
void Widget::on_createbinButton_clicked()
{
ui->outname->addItem("开始制作mp3 bin文件");
QDir *dir=new QDir(qApp->applicationDirPath());
int count = 0;
QString sizedata = "unsigned int data[] = {";
QString FilePath = qApp->applicationDirPath();
QString creathPath = FilePath + "/wavres.h";
QString creatbinPath = FilePath + "/wav.bin";
QFile::remove(creathPath);//删除文件
QFile::remove(creatbinPath);//删除文件
//下面开始创建文件写入内容
QFile creathfile(creathPath);
if(!creathfile.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append))
{
ui->outname->addItem("创建.h文件失败");
}
QTextStream in(&creathfile); //写入文件
in<<"#ifndef __MP3RES_H_"<<"\n";
in<<"#define __MP3RES_H_"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n"<<"\n";
in<<"typedef enum"<<"\n";
in<<"{"<<"\n";
// creathfile.close();
QFile creatbin(creatbinPath);
if(!creatbin.isOpen())
{
if (!creatbin.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append))
{
ui->outname->addItem("创建.bin文件失败");
return ;
}
}
foreach (QFileInfo mfi, dir->entryInfoList())
{
QString path = qApp->applicationDirPath();
QString RelativeRPath,ResultPath;
int pathSize;
if(mfi.isFile() && mfi.suffix()=="mp3") //判断文件不为空,后缀名为 .mp3 的文件
{
//count++;
pathSize = path.size();
path = mfi.absoluteFilePath();
RelativeRPath = path.mid(pathSize + 1);
ResultPath = QString(" %1").arg(RelativeRPath);
//ui->outname->addItem(path);
ResultPath = ResultPath.left(ResultPath.length() - 4);
ResultPath += ",";
//ResultPath = ResultPath.left(50);
//ResultPath += QString("%1").sprintf("%d", count);
//QTextStream in(&creathfile); //写入文件
in<<ResultPath<<"\n";
// creathfile.close();
QFile readFile(path);
if(!readFile.isOpen())
{
if (!readFile.open(QIODevice::ReadOnly))
{
ui->outname->addItem("读取文件失败");
return ;
}
}
int fileSize = readFile.size();
uchar *buffer = (uchar *)malloc(fileSize);
memset(buffer,0,fileSize);
readFile.seek(0);
int readfilesize = readFile.read((char *)buffer,fileSize);
qDebug()<<"readfilesize ="<<readfilesize<<fileSize;
ui->outname->addItem(QString("mp3文件序号: %1").arg(count)+QString(" 文件大小: %1").arg(readfilesize));
creatbin.write((const char *)&count,4);
creatbin.write((const char *)&readfilesize,4);
count++;
sizedata += QString("%1").sprintf("%d", readfilesize);
sizedata += ", ";
// creatbin.write((char *)buffer,fileSize);
readFile.close();
}
}
creatbin.seek(512);
foreach (QFileInfo mfi, dir->entryInfoList())
{
QString path = qApp->applicationDirPath();
if(mfi.isFile() && mfi.suffix()=="mp3") //判断文件不为空,后缀名为 .mp3 的文件
{
path = mfi.absoluteFilePath();
QFile readFile(path);
if(!readFile.isOpen())
{
if (!readFile.open(QIODevice::ReadOnly))
{
ui->outname->addItem("读取文件失败");
return ;
}
}
int fileSize = readFile.size();
uchar *buffer = (uchar *)malloc(fileSize);
memset(buffer,0,fileSize);
readFile.seek(0);
int readfilesize = readFile.read((char *)buffer,fileSize);
qDebug()<<"readfilesize ="<<readfilesize<<fileSize;
creatbin.write((char *)buffer,fileSize);
}
}
creatbin.close();
in<<"}wav_file_id_t;"<<"\n";
sizedata = sizedata.left(sizedata.length() - 2);
in<<sizedata<<"};"<<"\n"<<"\n"<<"\n"<<"\n"<<"#endif "<<"\n";
creathfile.close();
ui->outname->addItem(QString("创建完成,共计mp3文件个数: %1").arg(count));
}
void Widget::on_delmp3_clicked()
{
QDir *dir=new QDir(qApp->applicationDirPath());
int count = 0;
foreach (QFileInfo mfi, dir->entryInfoList())
{
QString path = qApp->applicationDirPath();
if(mfi.isFile() && mfi.suffix()=="mp3") //判断文件不为空,后缀名为 mp3 的文件
{
count++;
path = mfi.absoluteFilePath();
QFile::remove(path);//删除文件
}
}
ui->outname->addItem(QString("删除mp3文件个数: %1").arg(count));
}
widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_createbinButton_clicked();
void initWindows();
void on_close_clicked();
void on_clear_clicked();
void creatidandsizefile();
void on_delwav_clicked();
void on_delmp3_clicked();
void on_wavcreat_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
界面
源码链接:跳转链接