分类: LINUX
2010-11-22 21:53:14
经过几天的努力,利用Qt4实现的前台界面,madplay实现的后台播放器总算是能正常运行了(在fs2410开发板上的运行界面如下),下面的笔记简要说明了实现方式。
1. main.cpp 文件
#include
#include"audio_player.h"
#include
#include
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));
audio_player myAudio;
myAudio.setFont(QFont("wenquanyi",12,QFont::Normal)); //设置字体
myAudio.show();
return app.exec();
}
2.Audio_player.h 文件
#ifndef _AUDIO_PLAYER_H_
#define _AUDIO_PLAYER_H_
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct Tag_Header{
char Header[3];
char ver;
char Revision;
char Flag;
char Size[4];
};
struct Tag_Trailer{
char Header[3];
char Title[30];
char Artist[30];
char Album[30];
char Year[4];
char Comment[28];
char Cenre[1];
};
class audio_player : public QDialog
{
Q_OBJECT
public:
audio_player(QWidget *parent = 0, Qt::WindowFlags f = 0);
void readInfo(QString file);
void Tag_GetTrailer(void);
void Tag_GetHeader(void);
private slots:
void slotStart();
void slotUp();
void slotDown();
void slotOpen();
void slotGetVol();
void slotShowTime();
private:
QToolButton *start;
QToolButton *up;
QToolButton *down;
QSlider *slider;
QHBoxLayout *keyLayout;
QGridLayout *mainLayout;
QLabel *mp3Title;
QLabel *mp3Art;
QLabel *mp3Album;
QLabel *mp3Time;
QLabel *mp3Length;
QTimer *timer;
QString fileName;
QString filePath;
QStringList musicList;
QString command;
QString backend;
QString Mp3Title_copy;
Tag_Header Mp3Header;
Tag_Trailer Mp3Trailer;
int nCount;
int index;
FILE *fp;
};
#endif
3. Audio_player.cpp 文件
#include"audio_player.h"
static int fd;
int tm =0; //分钟
int ts =0; //秒
audio_player::audio_player(QWidget *parent, Qt::WindowFlags f)
: QDialog(parent, f)
{
QPalette p = this->palette();
p.setColor(QPalette::Window,Qt::black); //设置背景色
this->setPalette(p);
timer = new QTimer(this);
mainLayout = new QGridLayout(this);
keyLayout = new QHBoxLayout();
mp3Title = new QLabel;
mp3Art = new QLabel;
mp3Album = new QLabel;
mp3Time = new QLabel;
mp3Length = new QLabel;
mp3Length->setText("04:23");
p.setColor(QPalette::WindowText,Qt::white); //设置前景色
mp3Title->setPalette(p);
mp3Art->setPalette(p);
mp3Album->setPalette(p);
mp3Time->setPalette(p);
mp3Length->setPalette(p);
start = new QToolButton; //开始
start->setText(tr("start"));
start->setIcon(QPixmap(":/images/3.png"));
start->setIconSize(QPixmap(":/images/3.png").size());
start->setAutoRaise(TRUE);
start->setToolButtonStyle(Qt::ToolButtonIconOnly);
up = new QToolButton; //上一曲
up->setText(tr("up"));
up->setIcon(QPixmap(":/images/1.png"));
up->setIconSize(QPixmap(":/images/1.png").size());
up->setAutoRaise(TRUE);
up->setToolButtonStyle(Qt::ToolButtonIconOnly);
down = new QToolButton; //下一曲
down->setText(tr("down"));
down->setIcon(QPixmap(":/images/5.png"));
down->setIconSize(QPixmap(":/images/5.png").size());
down->setAutoRaise(TRUE);
down->setToolButtonStyle(Qt::ToolButtonIconOnly);
slider = new QSlider(Qt::Horizontal); //音量
slider->setMinimum(40);
slider->setMaximum(120);
slider->setValue(80);
slider->setTickPosition(QSlider::TicksBelow);
p.setColor(QPalette::Window,Qt::blue);
slider->setPalette(p);
keyLayout->addWidget(mp3Time);
keyLayout->addWidget(up);
keyLayout->addWidget(start);
keyLayout->addWidget(down);
keyLayout->addWidget(mp3Length);
mainLayout->addWidget(mp3Title,0,0,Qt::AlignHCenter);
mainLayout->addWidget(mp3Art,1,0,Qt::AlignHCenter);
mainLayout->addWidget(mp3Album,2,0,Qt::AlignHCenter);
mainLayout->addWidget(slider,3,0);
mainLayout->addLayout(keyLayout,4,0);
connect(start,SIGNAL(clicked()),this,SLOT(slotOpen()));
connect(up,SIGNAL(clicked()),this,SLOT(slotUp()));
connect(down,SIGNAL(clicked()),this,SLOT(slotDown()));
connect(slider,SIGNAL(valueChanged(int)),SLOT(slotGetVol()));
connect(timer,SIGNAL(timeout()),this,SLOT(slotShowTime()));
fd =::open("/dev/mixer", O_RDWR); //打开mixer设备,用于音量调节
if (fd == - 1)
{
perror("unable to open /dev/mixer");
exit(1);
}
QDir dir("/mnt/udisk/mp3");
filePath = tr("/mnt/udisk/mp3/"); //设置播放目录
musicList = dir.entryList(QDir::Files,QDir::Name); //遍历播放目录下的所有歌曲
nCount = musicList.size(); //获得歌曲数目
fileName = static_cast
command=tr("./sbin/madplay ");
backend = tr(" &"); //后台播放
setWindowTitle(tr("Madplayer")); //设置窗口名称
slotStart(); //开始播放
this->sizeHint();
}
void audio_player::slotOpen()
{
QFileDialog fileDilog(this); //建立文件对话框
QStringList openList;
fileDilog.setFileMode(QFileDialog::AnyFile);
fileDilog.setDirectory (trUtf8("/mnt/udisk/mp3"));
fileDilog.setNameFilter(trUtf8("*.mp3")); //仅显示 .mp3 文件
fileDilog.setViewMode(QFileDialog::List); //以list方式显示
fileDilog.resize(320,240); //对话框尺寸
if (fileDilog.exec())
openList =fileDilog.selectedFiles();
fileName = static_cast
fileName.remove(0,15); //移除15个字符的路径,得到歌曲名
system("killall -9 madplay"); //关闭madplay 进程
slotStart();
}
int flag =1;
void audio_player::slotShowTime()
{
char disp_buf[5];
FILE *out;
char str[30];
ts++;
if(ts >= 60) {
tm++;
ts = 0;
}
if(tm >= 60) {
tm = 0;
ts = 0;
}
sprintf(disp_buf,"%02d:%02d",tm,ts);
QString text = disp_buf;
mp3Time->setText(text); //显示时间
/* 查看当前进程列表中是否有 madplay 进程存在,如果不存在,说明当前歌曲播放完毕,则启动播放下一曲
*/
system("ps |pgrep -l madplay >/usr/run.dat");
out = fopen ("/usr/run.dat", "r");
fread (str, sizeof (str), 1, out);
printf("%s\n\r",str);
if(str[0]<48 ) slotDown();
fclose (out);
}
void audio_player::slotGetVol()
{
int usr_volume;
usr_volume = slider->value(); //设置音量大小
ioctl(fd,MIXER_WRITE(0),&usr_volume); //调节音量
}
void audio_player::slotStart()
{
char *S;
readInfo(fileName);
tm =0;
ts =0;
if(flag!=1)
{system("killall -9 madplay");flag = 1;}
else
{
flag=0;
QString command_full = command;
command_full.append(filePath);
command_full.append(fileName);
command_full.append(backend);
S = (command_full.toAscii()).data();
printf("%s\n\r",S);
timer->start(1000); //设置定是器的时间为一秒
system(S);
}
}
void audio_player::slotUp()
{
QString command_full = command;
system("killall -9 madplay"); //停止播放当前歌曲
index = musicList.indexOf(fileName); //获取当前歌曲索引
index--; //获取上一曲索引
if(index < 0)
index = nCount-1;
/* 根据上一曲索引找到上一曲文件名 */
fileName = static_cast
flag = 1;
slotStart(); //播放
}
void audio_player::slotDown()
{
QString command_full = command;
system("killall -9 madplay");
index = musicList.indexOf(fileName);
index++;
if(index >(nCount-1))
index = 0;
fileName = static_cast
flag = 1;
slotStart();
}
void audio_player::Tag_GetTrailer(void)
{
fseek(fp,
fread(Mp3Trailer.Header,3,1,fp);
fseek(fp,
fread(Mp3Trailer.Title,30,1,fp);
fseek(fp,
fread(Mp3Trailer.Artist,30,1,fp);
fseek(fp,
fread(Mp3Trailer.Album,30,1,fp);
fseek(fp,
fread(Mp3Trailer.Year,4,1,fp);
fseek(fp,
fread(Mp3Trailer.Comment,28,1,fp);
fseek(fp,
fread(Mp3Trailer.Cenre,1,1,fp);
}
/* 读取mp3文件ID3标签 */
void audio_player::Tag_GetHeader(void)
{
fseek(fp,0,SEEK_SET);
fread(Mp3Header.Header,sizeof(Mp3Header.Header),1,fp);
fseek(fp,3,SEEK_CUR);
fread(&Mp3Header.ver,sizeof(Mp3Header.ver),1,fp);
fseek(fp,4,SEEK_CUR);
fread(&Mp3Header.Revision,sizeof(Mp3Header.Revision),1,fp);
fseek(fp,5,SEEK_CUR);
fread(&Mp3Header.Flag,sizeof(Mp3Header.Flag),1,fp);
fseek(fp,6,SEEK_CUR);
fread(Mp3Header.Size,sizeof(Mp3Header.Size),1,fp);
}
/* 获取mp3文件信息 */
void audio_player::readInfo(QString file)
{
QString local_ch;
char *S;
QString file_full = filePath;
file_full.append(file);
S = (file_full.toAscii()).data();
/* 以二进制格式打开当前文件进行分析 */
if((fp=fopen(S,"rb"))==NULL)
{
printf("ERROR:can't open the file!");
getchar();
exit(1);
}
Tag_GetTrailer();
Tag_GetHeader();
QString sbuf;
sbuf = (tr(Mp3Trailer.Title)).section(" " ,0,0);
mp3Title->setText(tr("%1").arg(sbuf,0)); //mp3文件标题
sbuf = (tr(Mp3Trailer.Artist)).section(" ",0,1);
mp3Art->setText(tr("%1").arg(sbuf,0)); //mp3 Artist
sbuf = (tr(Mp3Trailer.Album)).section(" ",0,1);
mp3Album->setText(tr("%1").arg(sbuf,0)); //mp3 Album
fclose(fp);
}