PlayWidget.cpp 4.9 KB

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