详解QT媒体播放器代码案例实现

移动开发
QT媒体播放器代码案例实现是本文要介绍的内容,主要是来了解并学习QT中关于播放器的应用,本文通过代码来实现QT中播放器的应用,具体内容来看详细代码。

QT媒体播放器代码案例实现是本文要介绍的内容,主要是来了解并学习QT中关于播放器的应用,本文通过代码来实现QT播放器的应用,具体内容来看详细代码。

可以全屏,解决布局问题,时间进度可以显示..代码尽量简单,基本***,注需要解码包

如果需要QT基于mplayer媒体播放器:,请参考查看。

  1. MAINWINDOW.cpp  
  2. #include"mainwindow.h"  
  3. #include"ui_mainwindow.h"  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  8. #include  
  9. #include  
  10. #include  
  11. #include  
  12. #include  
  13. #include  
  14. #include  
  15. #include"QFileDialog"  
  16. #include"qDebug"  
  17. #include"phonon"  
  18. #include"QLabel"  
  19. #include  
  20. #include  
  21. #include  
  22. #include"phonon/VideoWidget"  
  23. #include"QTime"  
  24. #include  
  25. #include  
  26. MainWindow::MainWindow(QWidget*parent):  
  27. QMainWindow(parent),  
  28. ui(newUi::MainWindow)  
  29. {  
  30. ui->setupUi(this);  
  31. ui->widget->installEventFilter(this);  
  32. media=newPhonon::MediaObject;  
  33. //media->setCurrentSource(Phonon::MediaSource("../Puppet.mpg"));  
  34. w1=newQWidget(this);  
  35. w3=newQWidget(this);  
  36. timeLcd=newQLCDNumber(this);  
  37. vwidget=newPhonon::VideoWidget(w1);  
  38. Phonon::createPath(media,vwidget);  
  39. vwidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);  
  40. aOutput=newPhonon::AudioOutput(Phonon::VideoCategory);  
  41. Phonon::createPath(media,aOutput);  
  42. //QLabel*l1=newQLabel(QString::fromLocal8Bit("播放进度:"));  
  43. //QLabel*l2=newQLabel(QString::fromLocal8Bit("音量:"));  
  44. volumeSlider=newPhonon::VolumeSlider;  
  45. volumeSlider->setAudioOutput(aOutput);  
  46. volumeSlider->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);  
  47. Phonon::SeekSlider*seekSlider=newPhonon::SeekSlider;  
  48. seekSlider->setMediaObject(media);  
  49. timeLcd->display("00:00");  
  50. QVBoxLayout*vLayout=newQVBoxLayout;  
  51. vLayout->addWidget(vwidget);  
  52. vwidget->resize(1024,768);  
  53. w1->setLayout(vLayout);  
  54. //QHBoxLayout*h1=newQHBoxLayout;  
  55. //h1->addWidget(l1);  
  56. //h1->addWidget(seekSlider);  
  57. //h1->addWidget(timeLcd);  
  58. //QHBoxLayout*h2=newQHBoxLayout;  
  59. //h2->addWidget(l2);  
  60. //h2->addWidget(volumeSlider);  
  61. //h2->addWidget(ui->widget_4);  
  62. //QVBoxLayout*vLayout1=newQVBoxLayout;  
  63. //vLayout1->addLayout(h1);  
  64. //vLayout1->addLayout(h2);  
  65. //w3->setLayout(vLayout1);  
  66. //QHBoxLayout*hLayout1=newQHBoxLayout;  
  67. //hLayout1->addWidget(w3);  
  68. //QVBoxLayout*vLayout3=newQVBoxLayout;  
  69. //vLayout3->addWidget(w1);  
  70. //vLayout3->addLayout(hLayout1);  
  71. //vLayout3->addWidget(ui->widget_4);  
  72. //w2=newQWidget(this);  
  73. //w2->setLayout(vLayout3);  
  74. this->setCentralWidget(w1);  
  75. QDockWidget*shapesDockWidget1=newQDockWidget(QString::fromLocal8Bit("播放进度"));  
  76. shapesDockWidget1->setWidget(seekSlider);  
  77. //shapesDockWidget1->setLayout(h2);  
  78. //shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea  
  79. //|Qt::RightDockWidgetArea);  
  80. addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget1);  
  81. QDockWidget*shapesDockWidget2=newQDockWidget(QString::fromLocal8Bit("播放时间"));  
  82. shapesDockWidget2->setWidget(timeLcd);  
  83. shapesDockWidget2->setMaximumSize(100,100);  
  84. //shapesDockWidget1->setLayout(h2);  
  85. //shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea  
  86. //|Qt::RightDockWidgetArea);  
  87. addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget2);  
  88. QDockWidget*shapesDockWidget3=newQDockWidget(QString::fromLocal8Bit("播放界面"));  
  89. shapesDockWidget3->setWidget(ui->widget_4);  
  90. //shapesDockWidget1->setLayout(h2);  
  91. //shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea  
  92. //|Qt::RightDockWidgetArea);  
  93. //addDockWidget(Qt::RightDockWidgetArea,shapesDockWidget3);  
  94. addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget3);  
  95. QDockWidget*shapesDockWidget=newQDockWidget(QString::fromLocal8Bit("音量"));  
  96. shapesDockWidget->setWidget(volumeSlider);  
  97. //shapesDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea  
  98. //|Qt::RightDockWidgetArea);  
  99. addDockWidget(Qt::BottomDockWidgetArea,shapesDockWidget);  
  100. ui->pushButton_5->setEnabled(false);  
  101. ui->pushButton_7->setEnabled(false);  
  102. //QAction*at=newQAction("退出全屏",vwidget);  
  103. //vwidget->addAction(at);  
  104. //vwidget->setAcceptDrops(true);  
  105. //at->setShortcuts(QList()<   
  106. //connect(at,SIGNAL(triggered()),vwidget,SLOT(exitFullScreen()));  
  107. QAction*at1=newQAction("退出全屏",vwidget);  
  108. vwidget->addAction(at1);  
  109. vwidget->setAcceptDrops(true);  
  110. at1->setShortcuts(QList()<   
  111. connect(at1,SIGNAL(triggered()),vwidget,SLOT(exitFullScreen()));  
  112. connect(media,SIGNAL(tick(qint64)),this,SLOT(tick(qint64)));  
  113. readSettings();  
  114. }  
  115. MainWindow::~MainWindow()  
  116. {  
  117. deleteui;  
  118. }  
  119. voidMainWindow::resizeEvent(QResizeEvent*)  
  120. {  
  121. //QBitmapmaskBitmap(width(),height());  
  122. //maskBitmap.clear();  
  123.  
  124. //QPainterp(&maskBitmap);  
  125. //p.setBrush(Qt::black);  
  126. //p.drawRoundRect(0,0,width(),height(),2,4);  
  127. //p.end();  
  128.  
  129. //setMask(maskBitmap);  
  130. }  
  131. voidMainWindow::mouseMoveEvent(QMouseEvent*event)  
  132. {  
  133. //if(event->buttons()&Qt::LeftButton){  
  134. ////move(event->globalPos()-m_dragPosition);  
  135. //move(event->globalPos()-m_dragPosition);  
  136. //event->accept();  
  137. //}  
  138.  
  139. }  
  140. voidMainWindow::mousePressEvent(QMouseEvent*event)  
  141. {  
  142. //if(event->button()==Qt::LeftButton){  
  143. //m_dragPosition=event->globalPos()-frameGeometry().topLeft();  
  144.  
  145. ////m_dragPosition=event->globalPos()-frameGeometry().topLeft();  
  146. //////ui->widget->move(m_dragPosition);  
  147. //event->accept();  
  148. //}  
  149. }  
  150. voidMainWindow::on_pushButton_2_clicked()  
  151. {  
  152. this->close();  
  153. }  
  154. voidMainWindow::on_pushButton_3_clicked()  
  155. {  
  156. this->showMinimized();  
  157. }  
  158. boolMainWindow::eventFilter(QObject*obj,QEvent*event)  
  159. {  
  160. if(obj==ui->widget){  
  161. if(event->type()==QEvent::MouseButtonPress){  
  162. QMouseEvent*ev=static_cast(event);  
  163. if(ev->buttons()&Qt::LeftButton)  
  164. {  
  165. m_dragPosition=ev->globalPos()-frameGeometry().topLeft();  
  166. ev->accept();  
  167. }  
  168. returntrue;  
  169. }elseif(event->type()==QEvent::MouseMove){  
  170. QMouseEvent*ev=static_cast(event);  
  171. if(ev->buttons()&Qt::LeftButton){  
  172. //move(event->globalPos()-m_dragPosition);  
  173. move(ev->globalPos()-m_dragPosition);  
  174. ev->accept();  
  175. }  
  176.  
  177. }  
  178. else{  
  179. returnfalse;  
  180. }  
  181.  
  182. }  
  183. elseif(obj==w1)  
  184. {  
  185. if(event->type()==QEvent::MouseButtonPress){  
  186. QMouseEvent*ev=static_cast(event);  
  187. if(ev->buttons()&Qt::LeftButton)  
  188. {  
  189. vwidget=newPhonon::VideoWidget(this);  
  190. vwidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto);  
  191. vwidget->exitFullScreen();  
  192. }  
  193. }  
  194. }  
  195. else{  
  196. //passtheeventontotheparentclass  
  197. returnQMainWindow::eventFilter(obj,event);  
  198. }  
  199. returntrue;  
  200. }  
  201. voidMainWindow::on_pushButton_4_clicked()  
  202. {  
  203. QStringfilename=QFileDialog::getOpenFileName(this,QString::fromLocal8Bit("文件类型"),".",
  204. QString::fromLocal8Bit("文问类型(*.*)"));  
  205. qDebug()<   
  206. media->setCurrentSource(Phonon::MediaSource(filename));  
  207. media->play();  
  208. this->setWindowTitle(filename);  
  209. ui->pushButton_5->setEnabled(false);  
  210. ui->pushButton_7->setEnabled(true);  
  211. w1->setFocus();  
  212. //this->ui->label->setText(filename);  
  213. }  
  214. voidMainWindow::on_pushButton_8_clicked()  
  215. {  
  216. QMessageBox::information(this,tr("AboutMdieaPlayer"),  
  217. tr("ID:Jammy0.03"));  
  218. }  
  219. voidMainWindow::on_pushButton_clicked()  
  220. {  
  221. vwidget->enterFullScreen();  
  222. }  
  223. voidMainWindow::on_pushButton_5_clicked()  
  224. {  
  225. media->play();  
  226. ui->pushButton_5->setEnabled(false);  
  227. ui->pushButton_7->setEnabled(true);  
  228. }  
  229. voidMainWindow::on_pushButton_7_clicked()  
  230. {  
  231. media->pause();  
  232.  
  233. ui->pushButton_7->setEnabled(false);  
  234. ui->pushButton_5->setEnabled(true);  
  235. }  
  236. voidMainWindow::on_pushButton_6_clicked()  
  237. {  
  238. //media->setCurrentSource(Phonon::MediaSource(this->ui->label->text()));  
  239. media->stop();  
  240. }  
  241. voidMainWindow::tick(qint64time)  
  242. {  
  243. QTimedisplayTime(0,(time/60000)%60,(time/1000)%60);  
  244. timeLcd->display(displayTime.toString("mm:ss"));  
  245. }  
  246. voidMainWindow::keyPressEvent(QKeyEvent*k)  
  247. {  
  248. if(k->key()==Qt::Key_Space)  
  249. {  
  250. if(ui->pushButton_5->isEnabled()==true)  
  251. {  
  252. media->pause();  
  253. ui->pushButton_5->setEnabled(false);  
  254. ui->pushButton_7->setEnabled(true);  
  255. }  
  256. elseif(ui->pushButton_5->isEnabled()==false)  
  257. {  
  258. media->play();  
  259. ui->pushButton_5->setEnabled(true);  
  260. ui->pushButton_7->setEnabled(false);  
  261. }  
  262. }  
  263. elseif(k->modifiers()==Qt::AltModifier&&k->key()==Qt::Key_Return)  
  264. {  
  265. vwidget->enterFullScreen();  
  266. }  
  267. //elseif(k->modifiers()==Qt::ShiftModifier&&k->key()==Qt::Key_A)  
  268. //{  
  269. //vwidget->enterFullScreen();  
  270. //}  
  271. }  
  272. voidMainWindow::writeSettings()  
  273. {  
  274. QSettingssettings("SoftwareInc.","IconEditor");  
  275. settings.beginGroup("mainWindow");  
  276. settings.setValue("size",size());  
  277. settings.setValue("state",saveState());  
  278. settings.endGroup();  
  279. }  
  280. voidMainWindow::readSettings()  
  281. {  
  282. QSettingssettings("SoftwareInc.","IconEditor");  
  283. settings.beginGroup("mainWindow");  
  284. resize(settings.value("size").toSize());  
  285. restoreState(settings.value("state").toByteArray());  
  286. settings.endGroup();  
  287. }  
  288. voidMainWindow::closeEvent(QCloseEvent*ex)  
  289. {  
  290. inta=QMessageBox::information(NULL,QString::fromLocal8Bit("对话框"),
  291. QString::fromLocal8Bit("是否需要退出"),
  292. QString::fromLocal8Bit("确定"),
  293. QString::fromLocal8Bit("取消"),NULL,0,-1);  
  294. if(a==0)  
  295. {  
  296. writeSettings();  
  297. ex->accept();  
  298. }  
  299. elseif(a==1)  
  300. {  
  301. ex->ignore();  
  302. }  
  303. }  
  304. ****************************************************  
  305. MAINWINDOW.H  
  306. #defineMAINWINDOW_H  
  307. #include  
  308. #include  
  309. #include  
  310. #include  
  311. namespaceUi{  
  312. classMainWindow;  
  313. }  
  314. classMainWindow:publicQMainWindow  
  315. {  
  316. Q_OBJECT  
  317. public:  
  318. explicitMainWindow(QWidget*parent=0);  
  319. ~MainWindow();  
  320. protected:  
  321. Phonon::MediaObject*media;  
  322. Phonon::VideoWidget*vwidget;  
  323. Phonon::AudioOutput*aOutput;  
  324. Phonon::VolumeSlider*volumeSlider;  
  325. Phonon::SeekSlider*seekSlider;  
  326. private:  
  327. Ui::MainWindow*ui;  
  328. QPointm_dragPosition;  
  329. protected:  
  330. voidresizeEvent(QResizeEvent*event);  
  331. voidmouseMoveEvent(QMouseEvent*event);  
  332. voidmousePressEvent(QMouseEvent*event);  
  333. booleventFilter(QObject*obj,QEvent*ev);  
  334. QWidget*w1;  
  335. QWidget*w2;  
  336. QWidget*w3;  
  337. QLCDNumber*timeLcd;  
  338. privateslots:  
  339. voidon_pushButton_6_clicked();  
  340. voidon_pushButton_7_clicked();  
  341. voidon_pushButton_5_clicked();  
  342. voidon_pushButton_clicked();  
  343. voidon_pushButton_8_clicked();  
  344. voidon_pushButton_4_clicked();  
  345. voidon_pushButton_3_clicked();  
  346. voidon_pushButton_2_clicked();  
  347. voidtick(qint64time);  
  348. voidkeyPressEvent(QKeyEvent*k);  
  349. voidwriteSettings();  
  350. voidreadSettings();  
  351. voidcloseEvent(QCloseEvent*ex);  
  352. };  
  353. #endif//MAINWINDOW_H 

小结:详解QT媒体播放器代码案例实现的内容介绍完了,通过QT播放器的代码实现内容,希望本篇QT播放器的内容能对你有所帮助!

责任编辑:zhaolei 来源: CSDN博客
相关推荐

2011-09-06 11:08:21

QT播放器Mplayer

2011-06-24 10:21:11

Qt phonon 多媒体

2011-09-06 11:25:08

2010-01-25 09:43:02

Chrome OS媒体播放器

2023-08-26 19:07:40

VLC旋转视频

2015-10-19 17:28:00

MPlayer媒体播放器开源

2021-02-24 14:04:23

Linux媒体播放器桌面

2011-07-26 09:16:16

Windows媒体播放器

2010-03-04 09:34:35

Ubuntu Oper

2011-09-05 18:08:01

MTK音频播放器

2022-12-13 07:27:22

Windows微软

2011-06-13 09:33:04

2011-06-27 11:23:21

Qt 音乐播放器

2017-03-01 14:01:31

android多媒体音乐代码

2021-09-30 05:34:21

Windows 11操作系统微软

2022-01-05 20:09:29

Windows 11操作系统微软

2024-01-30 20:00:14

VLC字幕

2011-07-20 16:21:20

iPhone 视频 播放器

2018-05-25 14:37:58

2023-03-06 16:20:08

视频播放器VLC
点赞
收藏

51CTO技术栈公众号