1、-1-嵌入式课程设计报告在Linux下开发音乐播放器李荣贵 141578一概述按课程要求,在Linux环境下开发了一款简易的音乐播放器软件,名为“Mini音乐播放器”,运用工具如下:操作系统:虚拟机中安装xubuntu-14.04.1IDE:QTCreator3.0.1该播放器具有如下功能:1.从本地导入歌曲到歌曲列表中2.清空歌曲列表3.删除歌曲列表中选中的歌曲4.用进度条显示歌曲播放的进度,并显示时间5.用“播放/暂停”、“停止”、“上一曲”、“下一曲”四个按钮对歌曲进行控制6.用滑动器对歌曲音量进行控制7.右键会弹出菜单,可以对程序进行相应的控制二项目展示1.虚拟机截图-2-2.项目开发
2、界面-3-3.软件发行后运行情况隐藏界面部件后-4-三程序设计源码分析接下来展示源代码中的核心代码,其中包括头文件以及cpp文件中的构造函数,源代码我已经上传到网上,有需要的可以下载,链接:http:/ classWidget;classWidget:publicQWidget Q_OBJECTpublic:-5-explicitWidget(QWidget*parent =0);Widget();privateslots:voidaddSongs();/该槽函数功能是从本地导入音乐文件voidplayPause();/该槽函数是用来响应“播放/暂停”按钮voidplayStop();/该槽函
3、数是用来响应“停止”按钮voidplayNext(); /该槽函数是用来响应“下一曲”按钮voidplayPrevious();/该槽函数是用来响应“上一曲”按钮voidposChanged(qint64pos); /当歌曲播放进度变化时,该槽函数响应,功能是让进度条与歌曲播放进度保持一致voiddurChanged(qint64dur);/该槽函数功能是当歌曲总长度改变(即切歌)时,设置进度条最大值为歌曲长度(以ms为单位)voidsetPos(intpos); /该槽函数功能是当拖动进度条改变进度时,设置歌曲的播放进度与之保持一致voidclearList();/清空歌曲列表voiddel
4、ectSong();/删除歌曲列表中选中的歌曲voidvolumeSilence();/响应点击音量按钮,进而设置静音/正常voidsetVol(intvol); /拖动音量滑动条时,设置歌曲音量与之保持一致voidhideShow(); /隐藏/显示界面上的部件voidredVolume(); /减小音量-6-voidaddVolume();/增大音量private:Ui:Widget*ui;private:QMediaPlayer* player; /QT中提供的用来保存源文件的类,该类提供大量API供调用QMediaPlaylist*playList;/QT提供的保存播放文件的队列/以下
5、均为弹出菜单中动作QAction*action_hideShow;QAction*action_addSongs;QAction*action_playPause;QAction*action_stop;QAction*action_previous;QAction*action_next;QAction*action_addVolume;QAction*action_redVolume;QAction*action_quit;protected:voidcontextMenuEvent(QContextMenuEvent*);/该函数为虚函数,对其进行重写,可实现右键单击界面时弹出菜单-7-
6、;#endif/WIDGET_HWidget.cpp-#include“widget.h“#include“ui_widget.h“#include#include#include#includeWidget:Widget(QWidget*parent):QWidget(parent),ui(newUi:Widget) ui-setupUi(this);/下面几行作用是插入一张图片到播放器界面作为背景this-setAutoFillBackground(true);QPalettepalette;palette.setBrush(QPalette:Background,QBrush(QPixm
7、ap(“:/images/interface.jpg“);-8-this-setPalette(palette);ui-volumeSlider-setRange(0,100);this-setWindowTitle(tr(“mini音乐播放器“);this-playList=newQMediaPlaylist(this);this-player=newQMediaPlayer(this);player-setPlaylist(playList); /将player与playList连接player-setVolume(50);ui-volumeSlider-setValue(50);/以下几
8、行设置按钮的属性ui-addSongsButton-setStyleSheet(“QPushButtonborder-radius:5px;border-width:0px;“);ui-clearButton-setStyleSheet(“QPushButtonborder-radius:5px;border-width:0px;“);ui-delectButton-setStyleSheet(“QPushButtonborder-radius:5px;border-width:0px;“);ui-volumeButton-setStyleSheet(“QToolButtonborder-ra
9、dius:5px;border-width:0px;“);/信号与槽的连接QObject:connect(ui-volumeSlider,SIGNAL(valueChanged(int),this,SL-9-OT(setVol(int);QObject:connect(ui-addSongsButton,SIGNAL(clicked(),this,SLOT(addSongs();QObject:connect(ui-playPauseButton,SIGNAL(clicked(),this,SLOT(playPause();QObject:connect(ui-stoptButton,SIGN
10、AL(clicked(),this,SLOT(playStop();QObject:connect(ui-previousButton,SIGNAL(clicked(),this,SLOT(playPrevious();QObject:connect(ui-nextButton,SIGNAL(clicked(),this,SLOT(playNext();QObject:connect(player,SIGNAL(durationChanged(qint64),this,SLOT(durChanged(qint64);QObject:connect(player,SIGNAL(positionC
11、hanged(qint64),this,SLOT(posChanged(qint64);QObject:connect(ui-processSlider,SIGNAL(valueChanged(int),this,SLOT(setPos(int);QObject:connect(ui-clearButton,SIGNAL(clicked(),this,SLOT(clearList();QObject:connect(ui-delectButton,SIGNAL(clicked(),this,SLOT(delectSong();QObject:connect(ui-volumeButton,SI
12、GNAL(clicked(),this,SLOT(volu-10-meSilence();/设置按钮属性ui-stoptButton-setStyleSheet(“border:2pxgroovegray;border-radius:10px;padding:2px4px;“);ui-playPauseButton-setStyleSheet(“border:2pxgroovegray;border-radius:10px;padding:2px4px;“);ui-previousButton-setStyleSheet(“border:2pxgroovegray;border-radius:
13、10px;padding:2px4px;“);ui-nextButton-setStyleSheet(“border:2pxgroovegray;border-radius:10px;padding:2px4px;“);/构造相应的动作,用于右键弹出对话框action_hideShow=newQAction(tr(“隐藏/显示窗口部件“),this);QObject:connect(action_hideShow,SIGNAL(triggered(),this,SLOT(hideShow();action_addSongs=newQAction(tr(“添加本地歌曲“),this);actio
14、n_addSongs-setShortcut(tr(“Ctrl+I“);QObject:connect(action_addSongs,SIGNAL(triggered(),this,SLOT(addSongs();action_playPause=newQAction(tr(“播放/暂停“),this);action_playPause-setShortcut(tr(“Ctrl+Enter“);- 11-QObject:connect(action_playPause,SIGNAL(triggered(),this,SLOT(playPause();action_stop=newQActio
15、n(tr(“停止“),this);action_stop-setShortcut(tr(“Ctrl+S“);QObject:connect(action_stop,SIGNAL(triggered(),this,SLOT(playStop(); action_previous=newQAction(tr(“上一曲“),this);action_previous-setShortcut(tr(“Ctrl+Left“);QObject:connect(action_previous,SIGNAL(triggered(),this,SLOT(playPrevious();action_next=ne
16、wQAction(tr(“下一曲“),this);action_next-setShortcut(tr(“Ctrl+Right“);QObject:connect(action_next,SIGNAL(triggered(),this,SLOT(playNext(); action_addVolume=newQAction(tr(“音量+(5%)“),this);action_addVolume-setShortcut(tr(“Ctrl+Up“);QObject:connect(action_addVolume,SIGNAL(triggered(),this,SLOT(addVolume();
17、action_redVolume=newQAction(tr(“音量-(5%)“),this);action_redVolume-setShortcut(tr(“Ctrl+Down“);QObject:connect(action_redVolume,SIGNAL(triggered(),this,SLOT(redVolume();-12-action_quit=newQAction(tr(“退出“),this);QObject:connect(action_quit,SIGNAL(triggered(),this,SLOT(close();-四课程设计总结由于对Linux系统不熟悉,并且是第一次开发音乐播放器类软件,在开发过程中遇到了很多问题,有些问题在网上不能找到有效的解决办法,需要自己认真思考。通过这次的课程设计,我充分认识到在计算机领域实践的重要性,在实践中不断学习,认真思考,不断积累经验,才能人自己成长。