当前位置:   article > 正文

Qt简单视频播放器_qt播放mp4

qt播放mp4
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QMediaPlayer>
  5. #include <QVideoWidget>
  6. #include <QDir>
  7. #include <QListWidgetItem>
  8. namespace Ui {
  9. class Widget;
  10. }
  11. class Widget : public QWidget
  12. {
  13. Q_OBJECT
  14. public:
  15. explicit Widget(QWidget *parent = nullptr);
  16. ~Widget();
  17. void updateDir(QString fileName);
  18. private slots:
  19. void OnMetaDataAvailableChanged(bool available);
  20. void OnPositionChanged(qint64 value);
  21. void OnStateChanged(QMediaPlayer::State value);
  22. void on_btnPlay_clicked();
  23. void on_btnPrev_clicked();
  24. void on_btnNext_clicked();
  25. void on_btnVolume_clicked();
  26. void on_btnOpenFile_clicked();
  27. void on_btnOpenDir_clicked();
  28. void on_listWidget_itemDoubleClicked(QListWidgetItem *item);
  29. void on_sldPlay_sliderMoved(int position);
  30. void on_sldVolume_sliderMoved(int position);
  31. private:
  32. Ui::Widget *ui;
  33. QMediaPlayer *mediaPlayer; //多媒体对象变量声明
  34. QVideoWidget *videoWidget; //视频窗口
  35. QDir *dir;//文件
  36. int index;
  37. int stoped;
  38. int prevVolume;
  39. };
  40. #endif // WIDGET_H
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QDebug>
  4. #include <QTime>
  5. #include <QFileDialog>
  6. Widget::Widget(QWidget *parent) :
  7. QWidget(parent),
  8. ui(new Ui::Widget)
  9. {
  10. ui->setupUi(this);
  11. this->index = 0;
  12. this->stoped = 0;
  13. //实例化文件
  14. this->dir = new QDir();
  15. //实例化媒体
  16. this->mediaPlayer = new QMediaPlayer(this);
  17. // 实例化播放视频框框
  18. this->videoWidget = new QVideoWidget(this->ui->label);//设置视频父对象是标签
  19. this->videoWidget->resize(ui->label->size());//设置播放窗口大小
  20. // 将要播放的视频输出到播放视频框框中去
  21. this->mediaPlayer->setVideoOutput(this->videoWidget);
  22. this->ui->btnVolume->setText("");
  23. this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolume));
  24. this->ui->sldVolume->setRange(0, 100);
  25. this->ui->sldVolume->setValue(50);
  26. this->prevVolume = this->ui->sldVolume->value();
  27. this->mediaPlayer->setVolume(this->ui->sldVolume->value());//设置音量
  28. this->ui->sldPlay->setTracking(false);
  29. this->ui->sldVolume->setTracking(false);
  30. //信号链接槽函数
  31. //元数据改变
  32. connect(this->mediaPlayer, SIGNAL(metaDataAvailableChanged(bool)), this, SLOT(OnMetaDataAvailableChanged(bool)));
  33. //进度改变
  34. connect(this->mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(OnPositionChanged(qint64)));
  35. //状态改变
  36. connect(this->mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnStateChanged(QMediaPlayer::State)));
  37. //默认路径
  38. updateDir(R"(..\Video)");
  39. }
  40. Widget::~Widget()
  41. {
  42. delete ui;
  43. }
  44. void Widget::updateDir(QString filePath)
  45. {
  46. //设置路径
  47. this->dir->setPath(filePath);
  48. this->ui->listWidget->clear();
  49. this->index = 0;
  50. QStringList namelist;
  51. namelist << "*.mp4";
  52. if (!this->dir->exists())
  53. {
  54. return;
  55. }
  56. this->dir->setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); //文件 不要.和..
  57. this->dir->setSorting(QDir::DirsFirst);//文件夹排在前面
  58. QFileInfoList list = this->dir->entryInfoList(namelist);
  59. for (int i = 0; i < list.size(); ++i)
  60. {
  61. QListWidgetItem *item = nullptr;
  62. if (list.at(i).isFile())
  63. {
  64. item = new QListWidgetItem(this->ui->listWidget);
  65. item->setText(list.at(i).fileName());
  66. item->setIcon(QIcon(this->style()->standardIcon(QStyle::SP_FileIcon)));//标准图标
  67. }
  68. if (item != nullptr)
  69. {
  70. this->ui->listWidget->addItem(item);
  71. }
  72. }
  73. if (this->ui->listWidget->count() > 0)
  74. {
  75. QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
  76. this->stoped = 1;
  77. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
  78. this->stoped = 0;
  79. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  80. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  81. }
  82. else
  83. {
  84. this->stoped = 1;
  85. this->mediaPlayer->setMedia(QUrl::fromLocalFile(" "));
  86. this->stoped = 0;
  87. this->ui->labVideoName->setText("未知");
  88. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  89. }
  90. }
  91. void Widget::OnMetaDataAvailableChanged(bool available)
  92. {
  93. qDebug() << "OnMetaDataAvailableChanged";
  94. if (available)
  95. {
  96. qDebug() << "OnMetaDataAvailableChangedBool";
  97. int time = this->mediaPlayer->metaData("Duration").toInt();
  98. this->ui->labTotalTime->setText(QTime(0, 0, 0).addMSecs(time).toString("hh:mm:ss"));
  99. this->ui->sldPlay->setRange(0, time);//设置进度范围条
  100. }
  101. }
  102. void Widget::OnPositionChanged(qint64 value)
  103. {
  104. qDebug() << "OnPositionChanged";
  105. this->ui->labRealTime->setText(QTime(0, 0, 0).addMSecs(int(value)).toString("hh:mm:ss"));
  106. this->ui->sldPlay->setValue(int(value));
  107. }
  108. void Widget::OnStateChanged(QMediaPlayer::State value)
  109. {
  110. qDebug() << "OnStateChanged";
  111. if (QMediaPlayer::PlayingState == value)
  112. {
  113. qDebug() << "OnStateChanged播放";
  114. this->ui->btnPlay->setText("暂停");
  115. }
  116. else if (QMediaPlayer::PausedState == value)
  117. {
  118. qDebug() << "OnStateChanged暂停";
  119. this->ui->btnPlay->setText("播放");
  120. }
  121. else if (QMediaPlayer::StoppedState == value)
  122. {
  123. qDebug() << "OnStateChanged结束";
  124. qDebug() << this->stoped;
  125. this->ui->btnPlay->setText("播放");
  126. if (0 == this->stoped)
  127. {
  128. if ("顺序播放" == this->ui->cbxSelectMode->currentText())
  129. {
  130. qDebug() << "顺序播放";
  131. this->on_btnNext_clicked();
  132. }
  133. else if ("随机播放" == this->ui->cbxSelectMode->currentText())
  134. {
  135. qDebug() << "随机播放";
  136. if (ui->listWidget->count() != 1)
  137. {
  138. //设置随机数种子
  139. qsrand(uint(QTime(0, 0, 0).secsTo(QTime::currentTime())));
  140. while (1)
  141. {
  142. int rand = qrand() % this->ui->listWidget->count();
  143. if (rand != this->index)
  144. {
  145. this->index = rand;
  146. break;
  147. }
  148. }
  149. QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
  150. this->stoped = 1;
  151. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
  152. this->stoped = 0;
  153. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  154. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  155. }
  156. else
  157. {
  158. this->on_btnNext_clicked();
  159. }
  160. }
  161. else if ("循环播放" == this->ui->cbxSelectMode->currentText())
  162. {
  163. qDebug() << "循环播放";
  164. //啥也不做
  165. }
  166. this->mediaPlayer->play();
  167. }
  168. }
  169. }
  170. void Widget::on_btnPlay_clicked()
  171. {
  172. qDebug() << "on_btnPlay_clicked";
  173. if (this->ui->btnPlay->text() == "播放")
  174. {
  175. this->mediaPlayer->play();//开始播放
  176. }
  177. else if (this->ui->btnPlay->text() == "暂停")
  178. {
  179. this->mediaPlayer->pause();//暂停播放
  180. }
  181. }
  182. void Widget::on_btnPrev_clicked()
  183. {
  184. qDebug() << "on_btnPrev_clicked";
  185. //上一首
  186. --this->index;
  187. if (this->index < 0)
  188. {
  189. this->index = this->ui->listWidget->count() - 1;
  190. }
  191. //上一首名字
  192. QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
  193. //当前状态
  194. if (this->mediaPlayer->state() == QMediaPlayer::PlayingState)
  195. {
  196. this->stoped = 1;
  197. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
  198. this->stoped = 0;
  199. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  200. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  201. this->mediaPlayer->play();//保持播放状态
  202. return;
  203. }
  204. this->stoped = 1;
  205. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
  206. this->stoped = 0;
  207. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  208. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  209. }
  210. void Widget::on_btnNext_clicked()
  211. {
  212. qDebug() << "on_btnNext_clicked";
  213. //下一首
  214. ++this->index;
  215. if (this->index == this->ui->listWidget->count())
  216. {
  217. this->index = 0;
  218. }
  219. //下一首名字
  220. QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
  221. //当前状态
  222. if (this->mediaPlayer->state() == QMediaPlayer::PlayingState)
  223. {
  224. this->stoped = 1;
  225. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
  226. this->stoped = 0;
  227. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  228. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  229. this->mediaPlayer->play();//保持播放状态
  230. return;
  231. }
  232. this->stoped = 1;
  233. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
  234. this->stoped = 0;
  235. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  236. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  237. }
  238. void Widget::on_btnVolume_clicked()
  239. {
  240. qDebug() << "on_btnVolume_clicked";
  241. if (this->ui->sldVolume->value() != 0)
  242. {
  243. this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolumeMuted));
  244. this->ui->sldVolume->setValue(0);
  245. this->mediaPlayer->setVolume(0);
  246. }
  247. else
  248. {
  249. this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolume));
  250. this->ui->sldVolume->setValue(this->prevVolume);
  251. this->mediaPlayer->setVolume(this->prevVolume);
  252. }
  253. }
  254. void Widget::on_btnOpenFile_clicked()
  255. {
  256. qDebug() << "on_btnOpenDir_clicked";
  257. QString arg("MP4 (*.mp4)");
  258. QString fileNamePath = QFileDialog::getOpenFileName(this, "选择歌曲", R"(..\Music)", "MP4 (*.mp4)", &arg);
  259. if (fileNamePath == nullptr)
  260. {
  261. return;
  262. }
  263. this->ui->listWidget->clear();
  264. this->index = 0;
  265. QFileInfo fileInfo(fileNamePath);
  266. QListWidgetItem *item = new QListWidgetItem(this->ui->listWidget);
  267. item->setText(fileInfo.fileName());
  268. this->dir->setPath(R"(..\Music)");
  269. item->setIcon(QIcon(this->style()->standardIcon(QStyle::SP_FileIcon)));//标准图标
  270. if (item != nullptr)
  271. {
  272. this->ui->listWidget->addItem(item);
  273. }
  274. if (this->ui->listWidget->count() > 0)
  275. {
  276. this->stoped = 1;
  277. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileNamePath));
  278. this->stoped = 0;
  279. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  280. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  281. }
  282. else
  283. {
  284. this->stoped = 1;
  285. this->mediaPlayer->setMedia(QUrl::fromLocalFile(" "));
  286. this->stoped = 0;
  287. this->ui->labVideoName->setText("未知");
  288. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  289. }
  290. }
  291. void Widget::on_btnOpenDir_clicked()
  292. {
  293. qDebug() << "on_btnOpenDir_clicked";
  294. QString tmpPath = QFileDialog::getExistingDirectory();
  295. if (tmpPath == nullptr)
  296. {
  297. return;
  298. }
  299. updateDir(tmpPath);
  300. }
  301. void Widget::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
  302. {
  303. qDebug() << "on_listWidget_itemDoubleClicked";
  304. for (int i = 0; i < this->ui->listWidget->count(); ++i)
  305. {
  306. if (item->text() == this->ui->listWidget->item(i)->text())
  307. {
  308. this->index = i;
  309. }
  310. }
  311. //选定的名字
  312. QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
  313. //当前状态
  314. if (this->mediaPlayer->state() == QMediaPlayer::PlayingState)
  315. {
  316. this->stoped = 1;
  317. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
  318. this->stoped = 0;
  319. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  320. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  321. this->mediaPlayer->play();//保持播放状态
  322. return;
  323. }
  324. this->stoped = 1;
  325. this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
  326. this->stoped = 0;
  327. this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
  328. this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
  329. }
  330. void Widget::on_sldPlay_sliderMoved(int position)
  331. {
  332. qDebug() << "on_sldReal_sliderMoved";
  333. this->mediaPlayer->setPosition(position);//设置位置
  334. }
  335. void Widget::on_sldVolume_sliderMoved(int position)
  336. {
  337. qDebug() << "on_sldVolume_sliderMoved";
  338. this->prevVolume = position;
  339. this->mediaPlayer->setVolume(position);//设置音量
  340. if (0 == position)
  341. {
  342. this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolumeMuted));
  343. }
  344. else
  345. {
  346. this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolume));
  347. }
  348. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/778568
推荐阅读
相关标签
  

闽ICP备14008679号