基于Qt的简易多媒体播放器

项目描述:嵌入式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 }

 

转载于:https://www.cnblogs.com/luciaark/archive/2013/03/29/QT.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于QT完成的集计算器、电子相册、相机、记事本、多媒体音乐播放器、2048小游戏为一体的多媒体系统,有系统语音提示等。(遇到问题可以评论,注意注册和登录密码等数据默认保存在C盘下的Database文件夹下,使用前需先在C盘新建一个Database命名的文件夹) 项目使用软件: QtCreate5.12 项目使用模块: 数据库 GUI 界面设计 多媒体 摄像头 截图 文字转语音 动画显示 界面包含内容 一、 开机动画界面设计(自定义图片显示,图片放置欢迎字样) 二、 登录注册设计 注: 1. 注册 数据库+MD5 加密 让用户自由注册 a. 点击注册跳转到账户注册界面、点击注册中返回可以返回登录界面 b. 注册界面数据设置为不为空,用户名和用户账号不能有重复 c. 确认注册进行数据的插入,字段:用户名、性别、账户、密码 注册成功:消息盒子提示成功,清空输入框的内容 注册失败:消息盒子提示用户存在 d. 取消注册清空所有输入框内容 e. 限定输入框的输入长度,在对应的输入框设置提示内容 f. 如何确定选择的是男还是女提示:if 判断 ui-> QRadioButton->isChecked() 2. 登录 通过查阅数据库进行对比登录 成功登录:消息盒子提示成功,播报用户名,跳转到主界面 失败登录:消息盒子提示失败,清空账户和密码 3. 输入框设计为椭圆状,设置输入提示字符 4. 按钮设置点击和触摸时颜色切换效果或者使用图片作为背景 5. 界面背景设置为图片,标题设置为中文,图标设置为图片 三、 主界面设计 1. 按钮设置点击和触摸时颜色切换效果或者使用图片作为背景 2. 界面背景设置为图片,标题设置为中文,图标设置为图片 3. 时间显示控件自由选择 4. 进入其他界面操作时进行语音提示 5. 所有子界面能够返回主界面,在返回主界面时进行消息盒子提示 6. 设置所有子界面的标题文字

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值