| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include "PlayWidget.h"
- #include <QDebug>
- #include <QTimer>
- #include "playercontroller.h"
- #include "AvRecorder/ui/opengl_video_widget.h"
- #include "audio_effect_gl.h"
- PlayWidget::PlayWidget(QWidget* parent)
- : QWidget(parent)
- {
- setupUi();
- setupConnections();
- setPlayerController(new PlayerController);
- }
- void PlayWidget::setupUi()
- {
- m_videoWidget = std::make_unique<OpenGLVideoWidget>(this);
- m_sliderProgress = new QSlider(Qt::Horizontal, this);
- m_labelTime = new QLabel("00:00/00:00", this);
- m_btnPlayPause = new QPushButton("播放", this);
- m_comboSpeed = new QComboBox(this);
- m_comboSpeed->addItems({"0.5x", "1.0x", "1.5x", "2.0x"});
- m_sliderVolume = new QSlider(Qt::Horizontal, this);
- m_sliderVolume->setRange(0, 100);
- m_labelVolume = new QLabel("音量", this);
- QHBoxLayout* controlLayout = new QHBoxLayout;
- controlLayout->addWidget(m_btnPlayPause);
- controlLayout->addWidget(new QLabel("倍速:"));
- controlLayout->addWidget(m_comboSpeed);
- controlLayout->addWidget(m_labelVolume);
- controlLayout->addWidget(m_sliderVolume);
- m_audioEffect = std::make_unique<AudioEffectGL>(this);
- QVBoxLayout* mainLayout = new QVBoxLayout(this);
- mainLayout->addWidget(m_videoWidget.get(), 5);
- QHBoxLayout* progressLayout = new QHBoxLayout;
- progressLayout->addWidget(m_sliderProgress, 8);
- progressLayout->addWidget(m_labelTime, 2);
- mainLayout->addLayout(progressLayout);
- mainLayout->addLayout(controlLayout);
- mainLayout->addWidget(m_audioEffect.get(), 2);
- setLayout(mainLayout);
- }
- void PlayWidget::setupConnections()
- {
- connect(m_btnPlayPause, &QPushButton::clicked, this, &PlayWidget::onPlayPauseClicked);
- // connect(m_sliderProgress, &QSlider::sliderReleased, this, [this]() {
- // if (!m_playerController) return;
- // int value = m_sliderProgress->value();
- // int max = m_sliderProgress->maximum();
- // if (max > 0) {
- // double seek_time = value * m_totalTime / max;
- // m_playerController->videoSeek(seek_time);
- // }
- // });
- connect(m_comboSpeed, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &PlayWidget::onSpeedChanged);
- connect(m_sliderVolume, &QSlider::valueChanged, this, &PlayWidget::onVolumeChanged);
- }
- void PlayWidget::setPlayerController(PlayerController* controller)
- {
- m_playerController = controller;
- if (!m_playerController) return;
- // 视频帧信号
- connect(m_playerController, &PlayerController::frameReady, this, [this](AVFrame* frame) {
- if (m_videoWidget) m_videoWidget->Render(frame);
- });
- // 音频可视化信号
- connect(m_playerController, &PlayerController::audioData, this, [this](const AudioData& data) {
- if (m_audioEffect) m_audioEffect->paint_data(data);
- });
- // 进度/时间信号
- // connect(m_playerController, &PlayerController::updatePlayTime, this, [this]() {
- // if (!m_playerController) return;
- // auto state = m_playerController->state();
- // if (!state) return;
- // double cur = state->audio_clock;
- // double total = 0;
- // if (state->ic) total = state->ic->duration / 1000000.0;
- // m_totalTime = total;
- // int max = 1000;
- // m_sliderProgress->setMaximum(max);
- // m_sliderProgress->setValue(total > 0 ? int(cur * max / total) : 0);
- // QTime curT(0,0); curT = curT.addSecs(int(cur));
- // QTime totT(0,0); totT = totT.addSecs(int(total));
- // m_labelTime->setText(curT.toString("mm:ss") + "/" + totT.toString("mm:ss"));
- // });
- // 音量信号
- connect(m_playerController, &PlayerController::updatePlayControlVolume, this, [this]() {
- if (!m_playerController) return;
- float vol = m_playerController->deviceVolume();
- m_sliderVolume->setValue(int(vol * 100));
- });
- // 播放/暂停状态信号
- connect(m_playerController, &PlayerController::updatePlayControlStatus, this, [this]() {
- if (!m_playerController) return;
- auto state = m_playerController->state();
- if (state && state->paused)
- m_btnPlayPause->setText("播放");
- else
- m_btnPlayPause->setText("暂停");
- });
- controller->startToPlay("C:/Users/zhuizhu/Videos/1.mp4");
- }
- void PlayWidget::onPlayPauseClicked()
- {
- if (m_playerController) m_playerController->pausePlay();
- }
- void PlayWidget::onProgressChanged(int value)
- {
- // 进度条拖动时可选预览实现
- }
- void PlayWidget::onSpeedChanged(int index)
- {
- if (!m_playerController) return;
- double speed = 1.0;
- switch(index) {
- case 0: speed = 0.5; break;
- case 1: speed = 1.0; break;
- case 2: speed = 1.5; break;
- case 3: speed = 2.0; break;
- default: speed = 1.0; break;
- }
- m_playerController->setPlaySpeed(speed);
- }
- void PlayWidget::onVolumeChanged(int value)
- {
- if (!m_playerController) return;
- m_playerController->setDeviceVolume(value / 100.0f);
- }
|