PlayWidget.cpp 5.5 KB

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