前几天做了一个项目,需要自己实现一个播放器播放点播视频,用的QMediaPalyer;
一,准备:
QVBoxLayout *vboxLayoutInterface = new QVBoxLayout();
vboxLayoutInterface->setSpacing(0);
vboxLayoutInterface->setContentsMargins(0, 0, 0, 0);
player = new QMediaPlayer(this);//构造一个播放器对象
player->seekableChanged(true);
QVideoWidget *videoWidget = new QVideoWidget();//构造一个显示对象
videoWidget->setAspectRatioMode(Qt::KeepAspectRatioByExpanding);
player->setVideoOutput(videoWidget);//将播放器与显示器绑定
//videoWidget->show();
videoWidget->setFixedSize(640, 340);
labelCurTime = new QLabel();//当前时间标签
slider = new QSlider(Qt::Horizontal);//进度条
labelDuration = new QLabel();//总时长标签
QHBoxLayout *hlayoutProgress = new QHBoxLayout();
hlayoutProgress->setSpacing(0);
hlayoutProgress->setContentsMargins(0, 0, 0, 0);
hlayoutProgress->addWidget(labelCurTime);
hlayoutProgress->addWidget(slider);
hlayoutProgress->addWidget(labelDuration);
checkStart = new QCheckBox();//播放暂停按钮
checkStart->setFixedSize(30, 30);
checkStart->setObjectName("checkStart");
connect(checkStart, SIGNAL(clicked(bool)), SLOT(on_videoStarted(bool)));
QPushButton *btnRetry = new QPushButton();//停止按钮
btnRetry->setObjectName("btnRetry");
btnRetry->setFixedSize(30, 30);
connect(btnRetry, SIGNAL(clicked()), SLOT(on_videoRetry()));
QHBoxLayout *hlayoutControl = new QHBoxLayout();
hlayoutControl->setSpacing(0);
hlayoutControl->setContentsMargins(10, 0, 0, 0);
hlayoutControl->addWidget(checkStart);
hlayoutControl->addSpacing(10);
hlayoutControl->addWidget(btnRetry);
hlayoutControl->addStretch(1);
vboxLayoutInterface->addWidget(videoWidget);
vboxLayoutInterface->addLayout(hlayoutProgress);
vboxLayoutInterface->addLayout(hlayoutControl);
setLayout(vboxLayoutInterface);
setFixedSize(640, 400);
setStyleSheet(getQssContent(":/file/qss/player.qss"));
PlayerInit();
void .....::PlayerInit()
{
timer = new QTimer(this);//进度条刷新定时器
connect(timer, SIGNAL(timeout()), SLOT(on_setSliderValue()));
connect(slider, SIGNAL(sliderReleased()), SLOT(on_sliderReleased()));
player->setMedia(QUrl(m_media_url));//设置点播地址
player->setVolume(100);//设置音量
labelCurTime->setText("00:00:00");
labelDuration->setText("00:00:00");
}
二,分功能实现
void .......::on_videoStarted(bool checked)
{
if (player->state() == QMediaPlayer::State::PlayingState)
{//暂停
player->pause();
timer->stop();
}
else
{//开始
player->play();
qint64 duration = player->duration();
labelDuration->setText(QString("%1:%2:%3").arg(duration / 1000 / 3600, 2, 10, QLatin1Char('0')).arg(duration / 1000 / 60 % 60, 2, 10, QLatin1Char('0')).arg(duration / 1000 % 60, 2, 10, QLatin1Char('0')));
slider->setRange(0, duration);
timer->start(1000);
}
void ......::on_videoRetry()
{//停止
player->setMedia(QUrl(m_media_url));
qint64 duration = player->duration();
labelDuration->setText(QString("%1:%2:%3").arg(duration / 1000 / 3600, 2, 10, QLatin1Char('0')).arg(duration / 1000 / 60 % 60, 2, 10, QLatin1Char('0')).arg(duration / 1000 % 60, 2, 10, QLatin1Char('0')));
slider->setRange(0, duration);
checkStart->setChecked(false);
if (!timer->isActive())
timer->start(1000);
}
void ......::on_setSliderValue()
{//播放时长显示
qint64 curPos = player->position();
slider->setValue(curPos);
labelCurTime->setText(QString("%1:%2:%3").arg(curPos / 1000 / 3600, 2, 10, QLatin1Char('0')).arg(curPos / 1000 / 60 % 60, 2, 10, QLatin1Char('0')).arg(curPos / 1000 % 60, 2, 10, QLatin1Char('0')));
if (curPos == player->duration())
timer->stop();
}
void ....::on_sliderReleased()
{//拖动
quint64 curPos = slider->value();
player->setPosition(curPos);
}
这个例子会遇到几个问题,解决方案在下面的一篇文章中https://mp.csdn.net/postedit/79620925