项目描述:嵌入式C++软件编程项目,独立完成基于Qt的简易多媒体播放器;利用Phonon多媒体框架,实现本地多媒体的播放、暂停、停止、音量调节,宽度、色调、饱和度、对比度的调节,播放画面的缩放等功能;熟悉QT下用C++进行图形界面的开发、槽机制的使用。
项目代码地址:
1 widget.h 2 3 #ifndef WIDGET_H 4 #define WIDGET_H 5 6 #include <QWidget> 7 #include <phonon> 8 9 class QMenu; 10 class QAction; 11 12 namespace Ui { 13 class Widget; 14 } 15 16 class Widget : public QWidget 17 { 18 Q_OBJECT 19 20 public: 21 explicit Widget(QWidget *parent = 0); 22 ~Widget(); 23 24 private slots: 25 void createContextMenu(); 26 void showContextMenu(const QPoint &); 27 void aspectChanged(QAction *action); 28 void scaleChanged(QAction *action); 29 void on_horizontalSlider_valueChanged(int value); 30 void on_horizontalSlider_3_valueChanged(int value); 31 void on_horizontalSlider_2_valueChanged(int value); 32 void on_horizontalSlider_4_valueChanged(int value); 33 34 private: 35 Ui::Widget *ui; 36 Phonon::VideoWidget *videoWidget; 37 QMenu *mainMenu; 38 }; 39 40 #endif // WIDGET_H
1 widget。cpp 2 3 #include <QMenu> 4 #include <QAction> 5 #include <QVBoxLayout> 6 #include <QToolBar> 7 8 #include "widget.h" 9 #include "ui_widget.h" 10 11 Widget::Widget(QWidget *parent) : 12 QWidget(parent), 13 ui(new Ui::Widget) 14 { 15 ui->setupUi(this); 16 17 //创建媒体库 18 Phonon::MediaObject *mediaObject = new Phonon::MediaObject(this); 19 videoWidget = new Phonon::VideoWidget(this); 20 Phonon::createPath(mediaObject,videoWidget); 21 22 Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory,this); 23 Phonon::createPath(mediaObject,audioOutput); 24 25 mediaObject->setCurrentSource(Phonon::MediaSource("myVideo.mp4")); 26 27 //创建播放进度滑块 28 Phonon::SeekSlider *seekSlider = new Phonon::SeekSlider(mediaObject,this); 29 30 //创建工具栏,包含类播放,暂停,停止动作,以及音量控制滑块 31 QToolBar *toolBar = new QToolBar(this); 32 QAction *playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay),tr("播放"),this); 33 connect(playAction,SIGNAL(triggered()),mediaObject,SLOT(play())); 34 35 QAction *pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause),tr("暂停"),this); 36 connect(pauseAction,SIGNAL(triggered()),mediaObject,SLOT(pause())); 37 38 QAction *stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop),tr("停止"),this); 39 connect(stopAction,SIGNAL(triggered()),mediaObject,SLOT(stop())); 40 41 Phonon::VolumeSlider *volumeSlider = new Phonon::VolumeSlider(audioOutput,this); 42 volumeSlider->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum); 43 44 toolBar->addAction(playAction); 45 toolBar->addAction(pauseAction); 46 toolBar->addAction(stopAction); 47 toolBar->addWidget(volumeSlider); 48 49 //创建布局管理器,将各个部件都添加到布局管理器中 50 QVBoxLayout *mainLayout = new QVBoxLayout; 51 videoWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); 52 mainLayout->addWidget(videoWidget); 53 mainLayout->addWidget(seekSlider); 54 mainLayout->addWidget(toolBar); 55 mainLayout->addWidget(ui->frame); 56 setLayout(mainLayout); 57 58 //设置Widget和VideoWidget都使用自定义上下文菜单 59 setContextMenuPolicy(Qt::CustomContextMenu); 60 videoWidget->setContextMenuPolicy(Qt::CustomContextMenu); 61 connect(videoWidget,SIGNAL(customContextMenuRequested(const QPoint &)),SLOT(showContextMenu(const QPoint &))); 62 connect(this,SIGNAL(customContextMenuRequested(const QPoint &)),SLOT(showContextMenu(const QPoint &))); 63 64 //创建上下文菜单 65 createContextMenu(); 66 } 67 68 Widget::~Widget() 69 { 70 delete ui; 71 } 72 73 //更改亮度 74 void Widget::on_horizontalSlider_valueChanged(int value) 75 { 76 videoWidget->setBrightness(value/10.0); 77 } 78 79 //更改色调 80 void Widget::on_horizontalSlider_3_valueChanged(int value) 81 { 82 videoWidget->setHue(value/10.0); 83 } 84 85 //更改饱和度 86 void Widget::on_horizontalSlider_2_valueChanged(int value) 87 { 88 videoWidget->setSaturation(value/10.0); 89 } 90 91 //更改对比度 92 void Widget::on_horizontalSlider_4_valueChanged(int value) 93 { 94 videoWidget->setContrast(value/10.0); 95 } 96 97 void Widget::createContextMenu() 98 { 99 mainMenu = new QMenu(this); 100 101 //创建"宽高比"子菜单 102 QMenu *aspectMenu = mainMenu->addMenu(tr("宽高比")); 103 QActionGroup *aspectGroup = new QActionGroup(aspectMenu); 104 connect(aspectGroup,SIGNAL(triggered(QAction *)),this,SLOT(aspectChanged(QAction *))); 105 aspectGroup->setExclusive(true); 106 107 QAction *aspectActionAuto = aspectMenu->addAction(tr("自动")); 108 aspectActionAuto->setCheckable(true); 109 aspectActionAuto->setChecked(true); 110 aspectGroup->addAction(aspectActionAuto); 111 112 QAction *aspectActionScale = aspectMenu->addAction(tr("缩放")); 113 aspectActionScale->setCheckable(true); 114 aspectGroup->addAction(aspectActionScale); 115 116 QAction *aspectAction16_9 = aspectMenu->addAction(tr("16:9")); 117 aspectAction16_9->setCheckable(true); 118 aspectGroup->addAction(aspectAction16_9); 119 120 QAction *aspectAction4_3 = aspectMenu->addAction(tr("4:3")); 121 aspectAction4_3->setCheckable(true); 122 aspectGroup->addAction(aspectAction4_3); 123 124 //创建"缩放模式"子菜单 125 QMenu *scaleMenu = mainMenu->addMenu(tr("缩放模式")); 126 QActionGroup *scaleGroup = new QActionGroup(scaleMenu); 127 connect(scaleGroup,SIGNAL(triggered(QAction *)),this,SLOT(scaleChanged(QAction *))); 128 scaleGroup->setExclusive(true); 129 130 QAction *scaleActionFit = scaleMenu->addAction(tr("保持宽高比")); 131 scaleActionFit->setCheckable(true); 132 scaleActionFit->setChecked(true); 133 scaleGroup->addAction(scaleActionFit); 134 135 QAction *scaleActionCrop = scaleMenu->addAction(tr("缩放和裁剪")); 136 scaleActionCrop->setCheckable(true); 137 scaleGroup->addAction(scaleActionCrop); 138 139 //创建"全屏"子菜单 140 QAction *fullScreenAction = mainMenu->addAction(tr("全屏")); 141 fullScreenAction->setCheckable(true); 142 connect(fullScreenAction,SIGNAL(triggered(bool)),videoWidget,SLOT(setFullScreen(bool))); 143 144 } 145 146 //显示上下文菜单 147 void Widget::showContextMenu(const QPoint &pos) 148 { 149 mainMenu->popup(videoWidget->isFullScreen() ? pos : mapToGlobal(pos)); 150 } 151 152 //设置宽高比 153 void Widget::aspectChanged(QAction *action) 154 { 155 if (action->text() == tr("16:9")) 156 videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatio16_9); 157 else if (action->text() == tr("缩放")) 158 videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatioWidget); 159 else if (action->text() == tr("4:3")) 160 videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatio4_3); 161 else 162 videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto); 163 } 164 165 //设置缩放模式 166 void Widget::scaleChanged(QAction *action) 167 { 168 if (action->text() == tr("缩放和裁剪")) 169 videoWidget->setScaleMode(Phonon::VideoWidget::ScaleAndCrop); 170 else 171 videoWidget->setScaleMode(Phonon::VideoWidget::FitInView); 172 }
1 man.cpp 2 3 #include <QApplication> 4 #include <QTextCodec> 5 #include "widget.h" 6 7 int main(int argc, char *argv[]) 8 { 9 QApplication a(argc, argv); 10 a.setApplicationName("Player"); 11 QTextCodec::setCodecForTr(QTextCodec::codecForLocale()); 12 Widget w; 13 w.show(); 14 15 return a.exec(); 16 }