PlayWidget.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "PlayWidget.h"
  2. #include <QDebug>
  3. #include <QTimer>
  4. #include "playercontroller.h"
  5. #include "AvRecorder/ui/opengl_video_widget.h"
  6. #include "audio_effect_gl.h"
  7. #include "qnamespace.h"
  8. PlayWidget::PlayWidget(QWidget* parent)
  9. : QWidget(parent)
  10. {
  11. setupUi();
  12. setupConnections();
  13. setPlayerController(new PlayerController);
  14. }
  15. PlayWidget::~PlayWidget()
  16. {
  17. if (m_playerController) {
  18. delete m_playerController;
  19. }
  20. }
  21. void PlayWidget::startToPlay(const QString& url)
  22. {
  23. if (m_playerController) {
  24. m_playerController->startToPlay(url);
  25. }
  26. }
  27. bool PlayWidget::isPlaying() const
  28. {
  29. if (m_playerController) {
  30. return m_playerController->isPlaying();
  31. }
  32. return false;
  33. }
  34. void PlayWidget::setupUi()
  35. {
  36. m_videoWidget = std::make_unique<OpenGLVideoWidget>(this);
  37. m_sliderProgress = new QSlider(Qt::Horizontal, this);
  38. m_labelTime = new QLabel("00:00/00:00", this);
  39. m_btnPlayPause = new QPushButton("播放", this);
  40. m_comboSpeed = new QComboBox(this);
  41. m_comboSpeed->addItems({"0.5x", "1.0x", "1.5x", "2.0x"});
  42. m_sliderVolume = new QSlider(Qt::Horizontal, this);
  43. m_sliderVolume->setRange(0, 100);
  44. m_labelVolume = new QLabel("音量", this);
  45. QHBoxLayout* controlLayout = new QHBoxLayout;
  46. controlLayout->addWidget(m_btnPlayPause);
  47. controlLayout->addWidget(new QLabel("倍速:"));
  48. controlLayout->addWidget(m_comboSpeed);
  49. controlLayout->addWidget(m_labelVolume);
  50. controlLayout->addWidget(m_sliderVolume);
  51. m_audioEffect = std::make_unique<AudioEffectGL>(this);
  52. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  53. mainLayout->addWidget(m_videoWidget.get(), 5);
  54. QHBoxLayout* progressLayout = new QHBoxLayout;
  55. progressLayout->addWidget(m_sliderProgress, 8);
  56. progressLayout->addWidget(m_labelTime, 2);
  57. mainLayout->addLayout(progressLayout);
  58. mainLayout->addLayout(controlLayout);
  59. mainLayout->addWidget(m_audioEffect.get(), 2);
  60. setLayout(mainLayout);
  61. }
  62. void PlayWidget::setupConnections()
  63. {
  64. connect(m_btnPlayPause, &QPushButton::clicked, this, &PlayWidget::onPlayPauseClicked);
  65. // connect(m_sliderProgress, &QSlider::sliderReleased, this, [this]() {
  66. // if (!m_playerController) return;
  67. // int value = m_sliderProgress->value();
  68. // int max = m_sliderProgress->maximum();
  69. // if (max > 0) {
  70. // double seek_time = value * m_totalTime / max;
  71. // m_playerController->videoSeek(seek_time);
  72. // }
  73. // });
  74. connect(m_comboSpeed, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &PlayWidget::onSpeedChanged);
  75. connect(m_sliderVolume, &QSlider::valueChanged, this, &PlayWidget::onVolumeChanged);
  76. }
  77. void PlayWidget::setPlayerController(PlayerController* controller)
  78. {
  79. m_playerController = controller;
  80. if (!m_playerController) return;
  81. // 视频帧信号
  82. connect(
  83. m_playerController,
  84. &PlayerController::frameReady,
  85. this,
  86. [this](AVFrame* frame) {
  87. AVFrame* copiedFrame = av_frame_clone(frame);
  88. // 添加空指针检查和frame有效性检查
  89. if (m_videoWidget && copiedFrame && copiedFrame->data[0] && copiedFrame->width > 0
  90. && copiedFrame->height > 0) {
  91. m_videoWidget->Render(copiedFrame);
  92. }
  93. },
  94. Qt::DirectConnection);
  95. // 音频可视化信号
  96. connect(m_playerController, &PlayerController::audioData, this, [this](const AudioData& data) {
  97. if (m_audioEffect)
  98. m_audioEffect->paint_data(data);
  99. });
  100. connect(m_playerController, &PlayerController::videoStopped, this, [this]() {
  101. if (m_videoWidget)
  102. m_videoWidget->showEndTip("播放结束啦~");
  103. });
  104. // VideoPlayThread* videoPlayThread = m_playerController->videoPlayThread();
  105. // 进度/时间信号
  106. // connect(m_playerController, &PlayerController::updatePlayTime, this, [this]() {
  107. // if (!m_playerController) return;
  108. // auto state = m_playerController->state();
  109. // if (!state) return;
  110. // double cur = state->audio_clock;
  111. // double total = 0;
  112. // if (state->ic) total = state->ic->duration / 1000000.0;
  113. // m_totalTime = total;
  114. // int max = 1000;
  115. // m_sliderProgress->setMaximum(max);
  116. // m_sliderProgress->setValue(total > 0 ? int(cur * max / total) : 0);
  117. // QTime curT(0,0); curT = curT.addSecs(int(cur));
  118. // QTime totT(0,0); totT = totT.addSecs(int(total));
  119. // m_labelTime->setText(curT.toString("mm:ss") + "/" + totT.toString("mm:ss"));
  120. // });
  121. // 音量信号
  122. connect(m_playerController, &PlayerController::updatePlayControlVolume, this, [this]() {
  123. if (!m_playerController) return;
  124. float vol = m_playerController->deviceVolume();
  125. m_sliderVolume->setValue(int(vol * 100));
  126. });
  127. // 播放/暂停状态信号
  128. connect(m_playerController, &PlayerController::updatePlayControlStatus, this, [this]() {
  129. if (!m_playerController) return;
  130. auto state = m_playerController->state();
  131. if (state && state->paused)
  132. m_btnPlayPause->setText("播放");
  133. else
  134. m_btnPlayPause->setText("暂停");
  135. });
  136. // controller->startToPlay("C:/Users/zhuizhu/Videos/1.mp4");
  137. }
  138. void PlayWidget::onPlayPauseClicked()
  139. {
  140. if (m_playerController) m_playerController->pausePlay();
  141. }
  142. void PlayWidget::onProgressChanged(int value)
  143. {
  144. // 进度条拖动时可选预览实现
  145. }
  146. void PlayWidget::onSpeedChanged(int index)
  147. {
  148. if (!m_playerController) return;
  149. double speed = 1.0;
  150. switch(index) {
  151. case 0: speed = 0.5; break;
  152. case 1: speed = 1.0; break;
  153. case 2: speed = 1.5; break;
  154. case 3: speed = 2.0; break;
  155. default: speed = 1.0; break;
  156. }
  157. m_playerController->setPlaySpeed(speed);
  158. }
  159. void PlayWidget::onVolumeChanged(int value)
  160. {
  161. if (!m_playerController) return;
  162. m_playerController->setDeviceVolume(value / 100.0f);
  163. }