基于qt的音乐播放器.doc_第1页
基于qt的音乐播放器.doc_第2页
基于qt的音乐播放器.doc_第3页
基于qt的音乐播放器.doc_第4页
基于qt的音乐播放器.doc_第5页
已阅读5页,还剩43页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于QT的音乐播放器界面:功能:从E:/Users/Administrator/Music/目录中添加所有.mp3和.wma类型的文件播放。支持歌词显示,要求歌词放入E:/Users/Administrator/Music/lrc文件夹下,并且和对应音乐的名字一致。支持最小化到托盘。程序:/*main.cpp*/#include musicplayer.h#include int main(int argc, char *argv) QApplication a(argc, argv); MusicPlayer w; w.show(); return a.exec();/*musicPlayer.h*/#ifndef MUSICPLAYER_H#define MUSICPLAYER_H#include #include #include #include #include #include #include #include #include #include #include lrc.hQT_BEGIN_NAMESPACEclass QSlider;class QLabel;QT_END_NAMESPACEenum PLAYMODE RANDOM,ORDER,SINGLE;class MusicPlayer : public QWidget Q_OBJECTpublic: MusicPlayer(QWidget *parent = 0); MusicPlayer();public slots: void play(); void next(); void setMuted(); void random(); void order(); void single(); void iconActivated(QSystemTrayIcon:ActivationReason reason);signals: void changeMuting(bool muting);private slots: void mediaStateChanged(QMediaPlayer:State state); void positionChanged(qint64 position); void durationChanged(qint64 duration); void setVolume(int vol); void updateDurationInfo(qint64 currentInfo); void handleError(QMediaPlayer:Error error);private: QMediaPlayer player; QToolButton *playButton; QToolButton *nextButton; QSlider *positionSlider; QToolButton *muteButton; QSlider *volumeSlider; QLabel *durationLabel; Lrc *lrcLabel; qint64 totalDuration; bool playerMuted; QStringList playlist; QString filename; int pos;/播放序号 PLAYMODE playmode;/播放模式 QString defaultdirname; void CreatTrayMenu(); void CreatTrayIcon(); QSystemTrayIcon *myTrayIcon; QMenu *myMenu; QAction *randomAction; QAction *orderAction; QAction *singleAction; QAction *nextAction; QAction *restoreWinAction; QAction *quitAction; void setMedia(QString file); void selectMedia(int mode); void addPlayList(QString dirname); void closeEvent(QCloseEvent *event);#endif / MUSICPLAYER_H/*musicPlayer.cpp*/#include musicPlayer.h#include #include MusicPlayer:MusicPlayer(QWidget *parent) : QWidget(parent) qsrand(QTime:currentTime().msec(); setFixedSize(300, 90); defaultdirname = E:/Users/Administrator/Music/;/默认播放列表文件夹 pos=0; playmode = ORDER; CreatTrayIcon(); playButton = new QToolButton; playButton-setEnabled(false); playButton-setIcon(style()-standardIcon(QStyle:SP_MediaPlay); connect(playButton, SIGNAL(clicked(), this, SLOT(play(); nextButton = new QToolButton; nextButton-setIcon(style()-standardIcon(QStyle:SP_MediaSkipForward); connect(nextButton, SIGNAL(clicked(), &player, SLOT(stop(); /注:stop会触发mediaStateChanged,从而next() positionSlider = new QSlider(Qt:Horizontal); positionSlider-setRange(0, 0); positionSlider-setEnabled(false); muteButton = new QToolButton(this); muteButton-setIcon(style()-standardIcon(QStyle:SP_MediaVolume); playerMuted = false; connect(muteButton, SIGNAL(clicked(), this, SLOT(setMuted(); volumeSlider = new QSlider(Qt:Horizontal); volumeSlider-setRange(0, 100); volumeSlider-setValue(50); connect(volumeSlider, SIGNAL(valueChanged(int), this, SLOT(setVolume(int); durationLabel = new QLabel; durationLabel-setSizePolicy(QSizePolicy:Preferred, QSizePolicy:Maximum); lrcLabel = new Lrc(this); lrcLabel-setSizePolicy(QSizePolicy:Preferred, QSizePolicy:Maximum); QBoxLayout *controlLayout = new QHBoxLayout; controlLayout-setMargin(0); controlLayout-addWidget(playButton); controlLayout-addWidget(nextButton); controlLayout-addWidget(durationLabel); controlLayout-addStretch(1); controlLayout-addWidget(muteButton); controlLayout-addWidget(volumeSlider); /controlLayout-setStretch(5,1); QBoxLayout *layout = new QVBoxLayout; layout-addWidget(positionSlider); layout-addLayout(controlLayout); layout-addWidget(lrcLabel); setLayout(layout); setVolume(50); connect(&player, SIGNAL(stateChanged(QMediaPlayer:State), this, SLOT(mediaStateChanged(QMediaPlayer:State); connect(&player, SIGNAL(positionChanged(qint64), this, SLOT(positionChanged(qint64); connect(&player, SIGNAL(durationChanged(qint64), this, SLOT(durationChanged(qint64); connect(&player, SIGNAL(positionChanged(qint64), this, SLOT(updateDurationInfo(qint64); connect(&player, SIGNAL(error(QMediaPlayer:Error), this, SLOT(handleError(QMediaPlayer:Error); addPlayList(defaultdirname);MusicPlayer:MusicPlayer() delete playButton; delete nextButton; delete positionSlider; delete muteButton; delete volumeSlider; delete durationLabel; delete lrcLabel; delete myTrayIcon; delete randomAction; delete orderAction; delete singleAction; delete nextAction; delete restoreWinAction; delete quitAction; delete myMenu;void MusicPlayer:setMedia(QString filename) if (!filename.isEmpty() int n = filename.length() - filename.lastIndexOf(/) - 1; QString videoname = filename.right(n); setWindowTitle(videoname); player.setMedia(QUrl:fromLocalFile(filename); playButton-setEnabled(true); QString lrcName = videoname; lrcName.chop(3); lrcName = QDir:toNativeSeparators(E:/Users/Administrator/Music/lrc/) + lrcName + lrc; lrcLabel-addLrcFile(lrcName); player.play(); void MusicPlayer:play() switch(player.state() case QMediaPlayer:PlayingState: player.pause(); break; default: player.play(); break; void MusicPlayer:next() switch(playmode) case ORDER: selectMedia(1); break; case SINGLE: selectMedia(2); break; default: selectMedia(3); void MusicPlayer:setMuted() playerMuted = !playerMuted; player.setMuted(playerMuted); volumeSlider-setDisabled(playerMuted); if(playerMuted) muteButton-setIcon(style()-standardIcon(QStyle:SP_MediaVolumeMuted); else muteButton-setIcon(style()-standardIcon(QStyle:SP_MediaVolume); void MusicPlayer:mediaStateChanged(QMediaPlayer:State state) switch(state) case QMediaPlayer:StoppedState: lrcLabel-setDuration(0); next(); break; case QMediaPlayer:PlayingState: lrcLabel-startLrc(); playButton-setIcon(style()-standardIcon(QStyle:SP_MediaPause); break; default: lrcLabel-pauseLrc(); playButton-setIcon(style()-standardIcon(QStyle:SP_MediaPlay); break; void MusicPlayer:positionChanged(qint64 position) positionSlider-setValue(position); lrcLabel-setDuration(position);void MusicPlayer:setVolume(int vol) player.setVolume(vol);void MusicPlayer:durationChanged(qint64 duration) positionSlider-setRange(0, duration); totalDuration = player.duration()/1000;void MusicPlayer:updateDurationInfo(qint64 currentInfo) QString tStr; currentInfo /= 1000; if (currentInfo | totalDuration) QTime currentTime(currentInfo/3600)%60, (currentInfo/60)%60, currentInfo%60, (currentInfo*1000)%1000); QTime totalTime(totalDuration/3600)%60, (totalDuration/60)%60, totalDuration%60, (totalDuration*1000)%1000); QString format = mm:ss; if (totalDuration 3600) format = hh:mm:ss; tStr = currentTime.toString(format) + / + totalTime.toString(format); durationLabel-setText(tStr);void MusicPlayer:handleError(QMediaPlayer:Error error) playButton-setEnabled(false); lrcLabel-setText(Error + QString:number(error);void MusicPlayer:addPlayList(QString dirname) QDir dir(dirname); QStringList filters; filters *.mp3 0) filename = playlistpos; setMedia(defaultdirname + filename); else lrcLabel-setText(没有音乐文件!); void MusicPlayer:CreatTrayMenu() nextAction = new QAction(下一首(&N),this); restoreWinAction = new QAction(还 原(&R),this); quitAction = new QAction(退 出(&Q),this); randomAction = new QAction(随机播放,this); orderAction = new QAction(顺序播放,this); singleAction = new QAction(单曲循环,this); randomAction-setCheckable(true); orderAction-setCheckable(true); singleAction-setCheckable(true); randomAction-setChecked(false); orderAction-setChecked(true); singleAction-setChecked(false); connect(randomAction,SIGNAL(triggered(),this,SLOT(random(); connect(orderAction,SIGNAL(triggered(),this,SLOT(order(); connect(singleAction,SIGNAL(triggered(),this,SLOT(single(); connect(nextAction,SIGNAL(triggered(),this,SLOT(next(); connect(restoreWinAction,SIGNAL(triggered(),this,SLOT(showNormal(); connect(quitAction,SIGNAL(triggered(),qApp,SLOT(quit(); myMenu = new QMenu(QWidget*)QApplication:desktop(); myMenu-addAction(randomAction); myMenu-addAction(orderAction); myMenu-addAction(singleAction); myMenu-addSeparator(); myMenu-addAction(nextAction); myMenu-addAction(restoreWinAction); myMenu-addSeparator(); /加入一个分离符 myMenu-addAction(quitAction);void MusicPlayer:CreatTrayIcon() CreatTrayMenu(); if (!QSystemTrayIcon:isSystemTrayAvailable()/判断系统是否支持系统托盘图标 return; myTrayIcon = new QSystemTrayIcon(this); myTrayIcon-setIcon(QIcon(F:/Qt/test/musicplayer/player.ico); /设置图标图片 setWindowIcon(QIcon(F:/Qt/test/musicplayer/player.ico); /把图片设置到窗口上 myTrayIcon-setToolTip(filename); /托盘时,鼠标放上去的提示信息 myTrayIcon-showMessage(tip,musicplayer,QSystemTrayIcon:Information,2000); myTrayIcon-setContextMenu(myMenu); /设置托盘上下文菜单 myTrayIcon-show(); this-connect(myTrayIcon,SIGNAL(activated(QSystemTrayIcon:ActivationReason),this,SLOT(iconActivated(QSystemTrayIcon:ActivationReason);void MusicPlayer:iconActivated(QSystemTrayIcon:ActivationReason reason) switch(reason) case QSystemTrayIcon:Trigger: case QSystemTrayIcon:DoubleClick: showNormal(); break; case QSystemTrayIcon:MiddleClick: myTrayIcon-showMessage(tip,musicplayer,QSystemTrayIcon:NoIcon,2000); break; case QSystemTrayIcon:Unknown: myTrayIcon-showMessage(tip,musicplayer,QSystemTrayIcon:NoIcon,2000); break; default: break; void MusicPlayer:closeEvent(QCloseEvent *event) if (myTrayIcon-isVisible() myTrayIcon-showMessage(tip,musicplayer,QSystemTrayIcon:NoIcon,2000); hide();/最小化 event-ignore(); else event-accept(); void MusicPlayer:selectMedia(int mode) switch(mode) case 1:/选择下一个 if(pos = 0) pos=playlist.size()-1; else pos-; filename = playlistpos; setMedia(defaultdirname + filename); break; case 2:/选择当前 setMedia(defaultdirname + filename); break; default:/随机选取 int n = qrand()%(playlist.size(); filename = playlistn; setMedia(defaultdirname + filename); break; void MusicPlayer:random() playmode = RANDOM; randomAction-setChecked(true); orderAction-setChecked(false); singleAction-setChecked(false);void MusicPlayer:order() playmode = ORDER; randomAction-setChecked(false); orderAction-setChecked(true); singleAction-setChecked(false);void MusicPlayer:single() playmode = SINGLE; randomAction-setChecked(false); orderAction-setChecked(false); singleAction-setChecked(true);/*lrc.h*/#ifndef LRC_H#define LRC_H#include #include #include class Lrc : public QLabel Q_OBJECTpublic: Lrc(QWidget *parent = 0); Lrc(); void addLrcFile(const QString& fn); void setDuration(qint64 dura); void startLrc(); void pauseLrc();private slots: void showLrc();private: QString filename; QString data; QTimer *timer; qint64 duration; bool hasLrc;#endif / LRC_H/*lrc.cpp*/#include lrc.h#include #include #include #include Lrc:Lrc(QWidget *parent) : QLabel(parent) duration = 0; hasLrc = false; timer =new QTimer(this); /连接信号与槽 connect(timer, SIGNAL(timeout(), this, SLOT(showLrc();Lrc:Lrc()void Lrc:addLrcFile(const QString& fn) filename = fn; QFile f(filename); if(!f.exists() setText(no lrc file!); hasLrc = false; timer-stop(); else if(!f.open(QFile:ReadOnly|QFile:Text) return; QTextStream out(&f); data = out.readAll(); f.close(); hasLrc = true; void Lrc:setDuration(qint64 dura) duration = dura;void Lrc:startLrc() if(hasLrc) timer-start(10); void Lrc:pauseLrc() if(hasLrc) timer-stop(); void Lrc:showLrc() if(hasLrc) duration += 10; QString tStr; QTime currentTime(0, (duration/60000)%60, duration/1000%60, duration%1000); QString format = mm:ss.zzz; tStr = currentTime.toString(format); tStr.chop(1); int pa = data.indexOf(tStr); if(pa0) QString lrc = data.mid(pa); int pb = lrc.indexOf(n); lrc = lrc.left(pb); pb = lrc.lastIndexOf(); lrc = lrc.remove(0, pb+1); setText(lrc); else setText(no lrc file!); 附件:大学本科生毕业论文(设计)规范一、毕业论文(设计)格式规范一份完整的毕业论文(设计)材料一般应包括下列内容:(一)题目;(二)目录;(三)论文主体(包括中英文摘要及关键词;正文;致谢;参考文献等);(四)附录。具体分述如下: (一)题目题目应力求简短、精确、有概括性,直接反映毕业论文(设计)的中心内容和学科特点。题目一般不超过20个汉字,如确有必要,可用副标题作补充。(二)目录毕业论文(设计)必须按其结构顺序编写目录,要求层次分明,体现文章展开的步骤和作者思路。目录格式是论文的结构层次,反映作者的逻辑思维能力,所用格式应全文统一,每一层次下的正文必须另起一行。目录独立成页,以章、节、小节来编排。(三) 论文主体1、中英文摘要及关键词摘要一般不分段,不用图表,以精炼的文字对毕业论文(设计)的内容、观点、方法、成果和结论进行高度概括,具有独立性和自含性,自成一篇短文,具有报导作用。中文摘要一般以200-300个字为宜。关键词是反映毕业论文(设计)内容主题的词或词组,一般35个。其中英文摘要与中文摘要基本对应,英文关键词之间用分号分开,最后一个关键词后不加任何标点。2、正文包括引言、正文、结论等部分。(1)引言引言也称前言、导论、导言、绪言、绪论等。它的作用是向读者初步介绍文章的背景和内容,通常包括以下几个方面:为什么写这篇文章,要解决什么问题;论文的主要观点;与课题相关的历史回顾;写作资料的来源、性质及其运用情况,论文的规划和简要内容;研究中的新发现;课题的意义等。(2)正文正文是论文的核心部分,是作者学术理论水平和创造性工作的综合体现,是作者运用掌握的材料与方法进行论证、得出结论的部分,其任务是分析问题和解决问题。根据不同论文研究的课题性质、研究方法的不同,理论型、实验型和描述型论文的正文格式和写法不尽相同,但他们的要求是一致的。即:主题明确:全文围绕主题展开讨论,不离题;论证充分:有观点、有思路、有材料、有说服力;结论清楚:研究导出的结论不含糊、易理解;逻辑严密:文字精炼流畅、条理清晰。(3)结论结论是论文要点的回顾和提高,是整个研究过程的结晶,是全篇论文的精髓。结论中应对本篇论文解决了什么问题,得出了什么规律,存在什么问题给出明确的回答。撰写结论时,要注意精炼准确、总结提高、前后呼应。3、致谢(无必要时可省略)以精练的文字,对在毕业论文(设计)工作中直接给予指导、帮助的人员表示谢意,言辞恳切,实事求是。4、参考文献毕业论文(设计)须在论文的最后列出参考文献。参考文献应以公开发表过的、作者真正阅读过的、与论文密切相关的或直接引用的为限,未发表过的论文、试验报告、内部资料等不宜列入。参考文献的列写必须严格按照毕业论文(设计)中引用的先后顺序依次列写。参考文献的列写格式,详见“毕业论文(设计)的书写规范与打印要求”。(四)附录(无附录时可省略)凡不宜收入正文中的、又有价值的内容可编入毕业论文的附录中。如:大号的设计图纸;篇幅较大的计算机程序(但以研究软件程序为主的毕业论文题目,其程序可作为正文的一部分);过长的公式推演过程。其它内容如译文及原文、专题调研报告、文献综述等可另行装订成册。二、毕业论文(设计)的书写规范与打印要求(一)书写规范1、 引用有关政策、方针性内容务必正确无误,不得泄漏国家和单位机密。2、使用普通语体文写作,体例统一,文句通顺,无语法错误,简化字符合规范,标点符号使用正确,符号的上下角标和数码要写清楚且位置准确。3、采用中华人民共和国国家标准(GB31003102-93)规定的计量单位和符号,单位用正体,符号用斜体。4、使用外文缩写代替一术语时,首次出现的,应用括号注明其含义,如CPU(Central Processing Unit,中央处理器)。5、国内工厂、机关、单位的名称等应使用全名,如不得把“大学”简写成“衡阳师院”或“衡师院”。6、公式应另起一行并居中书写,一行写不完的长公式,最好在等号处或在运算符号处转行。公式编号用圆括号括起,示于公式所在行的行末右端。公式编序可以全文统一,依前后次序编排,也可以分章节编排,但二者不能混用。文中公式、表格、图的编排应统一。7、文中引用某一公式时,应写成:“由式(5)可知”。8、文中表格可以全文统一编序,也可以逐章独立排序,表序必须连续。文中引用表格时,“表”在前,序号在后,如:“见表8”。 表格格式可采用三线表,表格的名称和编号应居中,并位于表格上方,表序在前,表名在后,其中空一格,表名末不加标点符号。如: 9、文中插图都应有名称和序号,可以全文统一编序,也可以逐章独立排序,图序必须连续。文中引用插图时,“图”在前,序号在后,如:“见图12”。图的名称和编号应居中并写于图的下方,图序在前,图名在后,其中空一格,末尾不加标点。如: 插图应用Word文档绘制,或用CAD绘制后插入,不得用铅笔、钢笔、圆珠笔等绘制(特殊情况除外)。10、“正文”中如对某一术语或情况需加解释而又不宜写入正文时,应在此“术语”或“情况”后引入注释符号,置于右上角,有多个注释时,应依次编号,如:、。11、参考文献的书写格式:参考文献采用宋体5号字。正文引用参考文献依次编序,其序号用方括号括起上标注出。如“效率可提高25%2”,表示此结果援引自文献2。各类参考文献的编排格式及示例如下:a. 专著、论文集、学位论文、报告序号作者.文献题名文献类型标识.出版地:出版者,出版年.起止页码.1刘国钧,陈绍业,王凤翥.图书馆目录M.北京:高等教育出版社,1957,15-18.2辛希孟.信息技术与信息服务国际研讨会论文集:A集C.北京:中国社会科学出版社,19943张筑生.微分半动力系统的不变集D.北京:北京大学数学研究所,1983.4冯西桥.核反应堆压力管道与压力容器的LBB分析R.北京:清华大学核能技术设计研究院.1997.b. 期刊文章序号作者.文献题名J.刊名,年,卷(期):起止页码.5何龄修.读顾城南明史J.中国史研究,1998,(3):167-173.6金显贺,王昌长,王忠东,等.一种用于在线检测局部放电的数字滤波技术J.清华大学学报(自然科学版),1993,33(4):

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论