mainwindow.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QFileDialog>
  4. MainWindow::MainWindow(QWidget *parent)
  5. : QMainWindow(parent)
  6. , ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. this->setWindowTitle(QString("Qt+ffmpeg视频播放(硬解码)"));
  10. m_playImages.append(ui->widget);
  11. m_playImages.append(ui->widget_3);
  12. m_playImages.append(ui->widget_4);
  13. m_playImages.append(ui->widget_5);
  14. m_playImages.append(ui->widget_6);
  15. m_playImages.append(ui->widget_7);
  16. for(int i = 0; i < m_playImages.count(); i++)
  17. {
  18. m_readThreads.append(new ReadThread);
  19. connect(m_readThreads.at(i), &ReadThread::updateImage, m_playImages.at(i), &PlayImage::updateImage); // 这里使用Qt::DirectConnection会大幅度降低性能
  20. }
  21. connect(m_readThreads.at(0), &ReadThread::playState, this, &MainWindow::on_playState);
  22. }
  23. MainWindow::~MainWindow()
  24. {
  25. delete ui;
  26. }
  27. /**
  28. * @brief 获取本地视频路径
  29. */
  30. void MainWindow::on_btn_file_clicked()
  31. {
  32. QString strName = QFileDialog::getOpenFileName(this, "选择播放视频~!", "/", "视频 (*.mp4 *.m4v *.mov *.avi *.flv);; 其它(*)");
  33. if(strName.isEmpty())
  34. {
  35. return;
  36. }
  37. ui->com_url->setCurrentText(strName);
  38. }
  39. /**
  40. * @brief 视频播放/停止
  41. */
  42. void MainWindow::on_btn_start_clicked()
  43. {
  44. if(ui->btn_start->text() == "开始播放")
  45. {
  46. for(int i = 0; i < m_readThreads.count(); i++)
  47. {
  48. m_readThreads.at(i)->setHWDecoder(ui->check_HW->isChecked());
  49. m_readThreads.at(0)->open("C:/Users/admin/Desktop/原文件/1.2023年度大事记.mp4");
  50. m_readThreads.at(1)->open("C:/Users/admin/Desktop/原文件/2.赵.mp4");
  51. m_readThreads.at(2)->open("https://www.douyu.com/96291?dyshid=1937d4e1-3332100244c16a702808d75e00031701&dyshci=1");
  52. }
  53. }
  54. else
  55. {
  56. for(int i = 0; i < m_readThreads.count(); i++)
  57. {
  58. m_readThreads.at(i)->close();
  59. }
  60. }
  61. }
  62. /**
  63. * @brief 视频暂停/继续
  64. */
  65. void MainWindow::on_btn_pause_clicked()
  66. {
  67. if(ui->btn_pause->text() == "暂停")
  68. {
  69. for(int i = 0; i < m_readThreads.count(); i++)
  70. {
  71. m_readThreads.at(i)->pause(true);
  72. }
  73. ui->btn_pause->setText("继续");
  74. }
  75. else
  76. {
  77. for(int i = 0; i < m_readThreads.count(); i++)
  78. {
  79. m_readThreads.at(i)->pause(false);
  80. }
  81. ui->btn_pause->setText("暂停");
  82. }
  83. }
  84. /**
  85. * @brief 根据视频播放状态切换界面设置
  86. * @param state
  87. */
  88. void MainWindow::on_playState(ReadThread::PlayState state)
  89. {
  90. if(state == ReadThread::play)
  91. {
  92. this->setWindowTitle(QString("正在播放:%1").arg(m_readThreads.at(0)->url()));
  93. ui->btn_start->setText("停止播放");
  94. }
  95. else
  96. {
  97. ui->btn_start->setText("开始播放");
  98. ui->btn_pause->setText("暂停");
  99. this->setWindowTitle(QString("Qt+ffmpeg视频播放(硬解码)"));
  100. }
  101. }