音视频播放 

这里简单的制作了一个音乐播放器,播放器的界面设计如下所示:

Step1:这里是界面对应的HTML文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MainWindow</class>
  4. <widget class="QMainWindow" name="MainWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>400</width>
  10. <height>300</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MainWindow</string>
  15. </property>
  16. <widget class="QWidget" name="centralWidget">
  17. <widget class="QPushButton" name="Play_Puase">
  18. <property name="geometry">
  19. <rect>
  20. <x>120</x>
  21. <y>160</y>
  22. <width>31</width>
  23. <height>31</height>
  24. </rect>
  25. </property>
  26. <property name="text">
  27. <string>Play</string>
  28. </property>
  29. </widget>
  30. <widget class="QPushButton" name="NextSong">
  31. <property name="geometry">
  32. <rect>
  33. <x>10</x>
  34. <y>150</y>
  35. <width>81</width>
  36. <height>27</height>
  37. </rect>
  38. </property>
  39. <property name="text">
  40. <string>Next Song</string>
  41. </property>
  42. </widget>
  43. <widget class="QLabel" name="label">
  44. <property name="geometry">
  45. <rect>
  46. <x>0</x>
  47. <y>0</y>
  48. <width>321</width>
  49. <height>51</height>
  50. </rect>
  51. </property>
  52. <property name="font">
  53. <font>
  54. <pointsize>28</pointsize>
  55. <italic>true</italic>
  56. <underline>false</underline>
  57. <strikeout>false</strikeout>
  58. </font>
  59. </property>
  60. <property name="cursor">
  61. <cursorShape>BlankCursor</cursorShape>
  62. </property>
  63. <property name="text">
  64. <string>Qt interface Demo!</string>
  65. </property>
  66. <property name="alignment">
  67. <set>Qt::AlignCenter</set>
  68. </property>
  69. </widget>
  70. <widget class="QDial" name="Volume">
  71. <property name="geometry">
  72. <rect>
  73. <x>180</x>
  74. <y>150</y>
  75. <width>50</width>
  76. <height>64</height>
  77. </rect>
  78. </property>
  79. <property name="value">
  80. <number>50</number>
  81. </property>
  82. </widget>
  83. <widget class="QSlider" name="SongChoose">
  84. <property name="geometry">
  85. <rect>
  86. <x>10</x>
  87. <y>210</y>
  88. <width>231</width>
  89. <height>29</height>
  90. </rect>
  91. </property>
  92. <property name="orientation">
  93. <enum>Qt::Horizontal</enum>
  94. </property>
  95. </widget>
  96. <widget class="QPushButton" name="PrevSong">
  97. <property name="geometry">
  98. <rect>
  99. <x>10</x>
  100. <y>180</y>
  101. <width>81</width>
  102. <height>27</height>
  103. </rect>
  104. </property>
  105. <property name="text">
  106. <string>Prev Song</string>
  107. </property>
  108. </widget>
  109. <widget class="QLabel" name="label_2">
  110. <property name="geometry">
  111. <rect>
  112. <x>0</x>
  113. <y>50</y>
  114. <width>171</width>
  115. <height>51</height>
  116. </rect>
  117. </property>
  118. <property name="font">
  119. <font>
  120. <pointsize>22</pointsize>
  121. <italic>true</italic>
  122. <underline>false</underline>
  123. <strikeout>false</strikeout>
  124. </font>
  125. </property>
  126. <property name="cursor">
  127. <cursorShape>BlankCursor</cursorShape>
  128. </property>
  129. <property name="text">
  130. <string>Music Player</string>
  131. </property>
  132. <property name="alignment">
  133. <set>Qt::AlignCenter</set>
  134. </property>
  135. </widget>
  136. <widget class="QLabel" name="label_3">
  137. <property name="geometry">
  138. <rect>
  139. <x>20</x>
  140. <y>110</y>
  141. <width>171</width>
  142. <height>21</height>
  143. </rect>
  144. </property>
  145. <property name="font">
  146. <font>
  147. <pointsize>10</pointsize>
  148. <italic>true</italic>
  149. <underline>false</underline>
  150. <strikeout>false</strikeout>
  151. </font>
  152. </property>
  153. <property name="cursor">
  154. <cursorShape>BlankCursor</cursorShape>
  155. </property>
  156. <property name="text">
  157. <string>Designed by : mm1994uestc</string>
  158. </property>
  159. <property name="alignment">
  160. <set>Qt::AlignCenter</set>
  161. </property>
  162. </widget>
  163. <widget class="QLabel" name="singer">
  164. <property name="geometry">
  165. <rect>
  166. <x>220</x>
  167. <y>220</y>
  168. <width>161</width>
  169. <height>21</height>
  170. </rect>
  171. </property>
  172. <property name="font">
  173. <font>
  174. <pointsize>10</pointsize>
  175. <italic>true</italic>
  176. <underline>false</underline>
  177. <strikeout>false</strikeout>
  178. </font>
  179. </property>
  180. <property name="cursor">
  181. <cursorShape>BlankCursor</cursorShape>
  182. </property>
  183. <property name="text">
  184. <string>Singer:</string>
  185. </property>
  186. <property name="alignment">
  187. <set>Qt::AlignCenter</set>
  188. </property>
  189. </widget>
  190. <widget class="QLabel" name="title">
  191. <property name="geometry">
  192. <rect>
  193. <x>260</x>
  194. <y>170</y>
  195. <width>61</width>
  196. <height>21</height>
  197. </rect>
  198. </property>
  199. <property name="font">
  200. <font>
  201. <pointsize>10</pointsize>
  202. <italic>true</italic>
  203. <underline>false</underline>
  204. <strikeout>false</strikeout>
  205. </font>
  206. </property>
  207. <property name="cursor">
  208. <cursorShape>BlankCursor</cursorShape>
  209. </property>
  210. <property name="text">
  211. <string>Title:</string>
  212. </property>
  213. <property name="alignment">
  214. <set>Qt::AlignCenter</set>
  215. </property>
  216. </widget>
  217. <widget class="QLabel" name="ArtCover">
  218. <property name="geometry">
  219. <rect>
  220. <x>260</x>
  221. <y>60</y>
  222. <width>90</width>
  223. <height>90</height>
  224. </rect>
  225. </property>
  226. <property name="text">
  227. <string> Art_Cover</string>
  228. </property>
  229. </widget>
  230. </widget>
  231. <widget class="QMenuBar" name="menuBar">
  232. <property name="geometry">
  233. <rect>
  234. <x>0</x>
  235. <y>0</y>
  236. <width>400</width>
  237. <height>25</height>
  238. </rect>
  239. </property>
  240. <widget class="QMenu" name="menuFile">
  241. <property name="title">
  242. <string>File</string>
  243. </property>
  244. <addaction name="actionOpenLocalMedia"/>
  245. </widget>
  246. <addaction name="menuFile"/>
  247. </widget>
  248. <widget class="QToolBar" name="mainToolBar">
  249. <attribute name="toolBarArea">
  250. <enum>TopToolBarArea</enum>
  251. </attribute>
  252. <attribute name="toolBarBreak">
  253. <bool>false</bool>
  254. </attribute>
  255. </widget>
  256. <widget class="QStatusBar" name="statusBar"/>
  257. <action name="actionOpenLocalMedia">
  258. <property name="text">
  259. <string>OpenLocalMedia</string>
  260. </property>
  261. </action>
  262. </widget>
  263. <layoutdefault spacing="6" margin="11"/>
  264. <resources/>
  265. <connections/>
  266. </ui>

这里使用了Qt的metadata的相关模块,需要调用到metadata的库

在MusicPlayer.pro文件中添加如下内容:

  1. QT += core gui multimedia

这样就添加了工程需要的音视频模块multimedia

Step2:我们需要在mainwindow.h头文件中添加每一个按键信号对应的槽函数声明以及工程中使用的多媒体对象的数据结构如下所示(mainwindow.h文件的内容):

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QMediaPlayer>
  6. #include <QMediaPlaylist>
  7. #include <QMultimedia>
  8. #include <QMediaMetaData>
  9. #include <QTimer>
  10.  
  11. namespace Ui {
  12. class MainWindow;
  13. }
  14. class MainWindow : public QMainWindow
  15. {
  16. Q_OBJECT
  17. public:
  18. explicit MainWindow(QWidget *parent = 0);
  19. ~MainWindow();
  20. private slots:
  21. void on_NextSong_clicked(bool checked); //下一首-槽函数声明
  22. void on_PrevSong_clicked(bool checked); // 上一首-槽函数声明
  23. void on_Volume_valueChanged(int value); // 音量改变的槽函数申明
  24. void on_SongChoose_sliderMoved(int position);
  25. void on_openlocal_media();
  26. void on_Play_Puase_clicked(bool checked);
  27. void on_playProgressUpdate();
  28. void on_MetaDateUpdate();
  29. private:
  30. Ui::MainWindow *ui;
  31. QMediaPlayer *mediaPlayer; // 多媒体对象变量声明
  32. QMediaPlaylist *localMediaPlaylist; // 多媒体对象列表对象声明
  33. QTimer *progressTimer; // 定时器对象声明
  34. };
  35. #endif // MAINWINDOW_H

Step3:对应的槽函数的实现:

 func1:on_NextSong_clicked   func2:on_PrevSong_clicked   func3:on_Volume_valueChanged   func4:on_SongChoose_sliderMoved

 func5:on_openlocal_media     func6:on_Play_Puase_clicked    func7:on_playProgressUpdate    func8:on_MetaDateUpdate

  1. void MainWindow::on_NextSong_clicked(bool checked)
  2. {
  3. qDebug() << "on_NextSong_clicked is pushed";
  4. this->mediaPlayer->playlist()->next();
  5. }
  6.  
  7. void MainWindow::on_PrevSong_clicked(bool checked)
  8. {
  9. qDebug() << "on_PrevSong_clicked is pushed";
  10. this->mediaPlayer->playlist()->previous();
  11. }
  12.  
  13. void MainWindow::on_Volume_valueChanged(int value)
  14. {
  15. qDebug()<< value;
  16. this->mediaPlayer->setVolume(value);
  17. }
  18.  
  19. void MainWindow::on_SongChoose_sliderMoved(int position)
  20. {
  21. qDebug()<< position;
  22. float percent = (position*1.0)/this->ui->SongChoose->maximum();
  23. long value = this->mediaPlayer->duration()*percent;
  24. this->mediaPlayer->setPosition(value);
  25. }
  26.  
  27. void MainWindow::on_openlocal_media()
  28. {
  29. QStringList fileNamelist;
  30. fileNamelist = QFileDialog::getOpenFileNames(this,tr("select local files"),"~/",tr("MP3/MP4 Files(*.mp3 *.mp4);;")); //Read file with Regex Rules.
  31. if(!fileNamelist.isEmpty())
  32. {
  33. qDebug() << fileNamelist;
  34. this->localMediaPlaylist->clear(); //Clear the PlayList
  35. foreach (const QString &fileName,fileNamelist) {
  36. QMediaContent media = QMediaContent(QUrl::fromLocalFile(fileName)); //Add the media into the PlayList
  37. this->localMediaPlaylist->addMedia(media);
  38. }
  39. this->localMediaPlaylist->setCurrentIndex(); //Set the Current media when program begining
  40. }else{
  41.  
  42. }
  43. return ;
  44. }
  45.  
  46. void MainWindow::on_Play_Puase_clicked(bool checked)
  47. {
  48. qDebug() << "Play or Pause?";
  49. if(this->mediaPlayer->state() == QMediaPlayer::PlayingState)
  50. {
  51. this->mediaPlayer->pause();
  52. }else
  53. {
  54. this->mediaPlayer->setVolume(this->ui->Volume->value()); //Choose current volume to be the current media!
  55. this->mediaPlayer->play();
  56. }
  57. }
  58.  
  59. void MainWindow::on_playProgressUpdate()
  60. {
  61. long pos = this->mediaPlayer->position();
  62. long duration = this->mediaPlayer->duration();
  63.  
  64. int value = (1.0*pos/duration)*;
  65.  
  66. this->ui->SongChoose->setValue(value);
  67. }
  68.  
  69. void MainWindow::on_MetaDateUpdate()
  70. {
  71. QString title,albumArtist;
  72. QImage coverImage;
  73. QPixmap pixmap;
  74. title = this->mediaPlayer->metaData("Title").toString();
  75. albumArtist = this->mediaPlayer->metaData("AudioCodec").toString();
  76. coverImage = this->mediaPlayer->metaData("CoverArtImage").value<QImage>();
  77. if(coverImage.isNull())
  78. {
  79. pixmap = QPixmap(":/images/MusicPlayerLogo.jpg");
  80. }else
  81. {
  82. pixmap.convertFromImage(coverImage);
  83. }
  84.  
  85. this->ui->title->setText(title);
  86. qDebug() << title;
  87. this->ui->singer->setText(albumArtist);
  88. qDebug() << albumArtist;
  89. this->ui->ArtCover->setPixmap(pixmap.scaled(this->ui->ArtCover->size()));
  90. }

Step4:完成槽函数的实现之后就将对应的型号和槽函数进行连接connect:

  1. connect(this->ui->NextSong,SIGNAL(clicked(bool)),this,SLOT(on_NextSong_clicked())); //Single connect to SLOT
  2. connect(this->ui->PrevSong,SIGNAL(clicked(bool)),this,SLOT(on_PrevSong_clicked()));
  3. connect(this->ui->Volume,SIGNAL(valueChanged(int)),this,SLOT(on_Volume_valueChanged()));
  4. connect(this->ui->SongChoose,SIGNAL(sliderMoved(int)),this,SLOT(on_SongChoose_sliderMoved()));
  5. connect(this->ui->actionOpenLocalMedia,SIGNAL(triggered(bool)),this,SLOT(on_openlocal_media()));
  6. connect(this->ui->Play_Puase,SIGNAL(clicked(bool)),this,SLOT(on_Play_Puase_clicked()));
  7. connect(this->progressTimer,SIGNAL(timeout()),this,SLOT(on_playProgressUpdate()));
  8. connect(this->mediaPlayer,SIGNAL(metaDataChanged()),this,SLOT(on_MetaDateUpdate()));

Step5:初始化在mainwindow.h头文件中定义的变量:

  1. this->mediaPlayer = new QMediaPlayer(this);
  2. this->localMediaPlaylist = new QMediaPlaylist(this);
  3. this->mediaPlayer->setPlaylist(this->localMediaPlaylist);
  4. this->mediaPlayer->setVolume(50); //Set default Volume Value
  5.  
  6. this->progressTimer = new QTimer(this);
  7. this->progressTimer->setInterval(100); //100ms
  8. this->progressTimer->start();

完成以上工作即可编译运行整个工程,这就是简单的多媒体播放器的制作。

6、Qt Project之音视频播放的更多相关文章

  1. Pyqt 音视频播放器

    在寻找如何使用Pyqt做一个播放器时首先找到的是openCV2 openCV2 貌似太强大了,各种关于图像处理的事情它都能完成,如 读取摄像头.图像识别.人脸识别.  图像灰度处理 . 播放视频等,强 ...

  2. Qt+FFmpeg 简单实现视频播放

    这里使用 Qt + FFmpeg 实现了一个简单播放视频的例子.先看下按下按钮播放视频时的效果图: 完整工程下载链接:Github-FFmpeg_demo 注意:一定要将 bin 目录下的 dll 文 ...

  3. Android音视频之MediaPlayer音视频播放

    前言: 昨天总结了视频录制,今天来学习一下视频的播放,Android的视频播放主要采用MediaPlayer类. MediaPlayer介绍 MediaPlayer类可用于控制音频/视频文件或流的播放 ...

  4. iOS AVKit音视频播放全面详解

    公司项目中经常要用到音视频处理,也需要去定制一些东西,然后整理这些音视频处理就显得尤为重要!方便自己和广大朋友学习收藏! 以下参考连接特别重要: 苹果官方:AVKit API 苹果官方:AVFound ...

  5. iOS - AVPlayer 音视频播放

    前言 NS_CLASS_AVAILABLE(10_7, 4_0) @interface AVPlayer : NSObject @available(iOS 4.0, *) public class ...

  6. 在CentOS下面编译WizNote Qt Project

    编译环境 CentOS 64位 Desktop 版本:6.5 编译前的准备: CentOS的用户默认没有root权限,如果当前用户没有权限root,则可以在终端里面执行下面的命令: su root s ...

  7. Qt Project的持续集成方案

    作者:齐亮链接:http://www.zhihu.com/question/24314354/answer/27547787来源:知乎著作权归作者所有,转载请联系作者获得授权. PETER HARTM ...

  8. Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project

    Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project Qt WebKit and HTML5 geolocation   I ...

  9. DOM Traversal Example | Documentation | Qt Project

    DOM Traversal Example | Documentation | Qt Project DOM Traversal Example

随机推荐

  1. HashMap遍历的两种方式,推荐使用entrySet()

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) {    ...

  2. 玩转 lua in Redis

    一.引言 Redis学了一段时间了,基本的东西都没问题了.从今天开始讲写一些redis和lua脚本的相关的东西,lua这个脚本是一个好东西,可以运行在任何平台上,也可以嵌入到大多数语言当中,来扩展其功 ...

  3. 一切皆Socket

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. socket()函数介绍 socket函数介绍 函数原型 domai ...

  4. Inspector did not run successfully.

    装虚拟机,卡在这个报错1天了, server没有问题,其余所有的agent都不能运行. 这部分没有日志,只有单纯的报错信息,omg,百度.bing一顿骚操作,还是没有解决问题. 因为默认安装jdk1. ...

  5. Hbase理论&&hbase shell&&python操作hbase&&python通过mapreduce操作hbase

    一.Hbase搭建: 二.理论知识介绍: 1Hbase介绍: Hbase是分布式.面向列的开源数据库(其实准确的说是面向列族).HDFS为Hbase提供可靠的底层数据存储服务,MapReduce为Hb ...

  6. nodejs之koa-router与koa-body搭配使用

    简介 koa需要搭配中间件来做接口更方便,使用Koa-body & Koa-router 使用 koa2 创建接口,处理post请求 const koa=require("koa&q ...

  7. asp.net core ioc 依赖注入

    1.生命周期 内置的IOC有三种生命周期: Transient: Transient服务在每次被请求时都会被创建.这种生命周期比较适用于轻量级的无状态服务. Scoped: Scoped生命周期的服务 ...

  8. CSS3 伸缩布局盒模型

    CSS3引入的布局模式Flexbox布局,主要思想是让容器有能力让其子项目能够改变其宽度,高度,以最佳方式填充可用空间.Flex容器使用Flex项目可以自动放大与收缩,用来填补可用的空闲空间.更重要的 ...

  9. CAS5.3.X 配置备忘

    ## # 普通MD5用户jdbc验证 ## #配置数据库连接 cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver cas.auth ...

  10. Doracle.jdbc.J2EE13Compliant=true

    To make the Oracle driver behave in a Java EE-compliant manner, you must define the following JVM pr ...