PlayWidget.cpp 5.1 KB

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