QMediaPlayer 例子

前几天做了一个项目,需要自己实现一个播放器播放点播视频,用的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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值