大家好,又见面了,我是你们的朋友全栈君。
播放效果
1.pro文件增加项
……
QT += multimedia multimediawidgets
……..
2.头文件
void init();
void positionChanged(qint64 position);
void durationChanged(qint64 duration);
void on_playButton_clicked();
void updateDurationInfo(qint64 currentInfo);
void on_treeView_player_doubleClicked(const QModelIndex &index);//这个是绑定文件双击打开,当然也可以修改成打开本地文件播放的形式。
3. .cpp文件 (UI->调的控件都要在ui界面里设置好,再布置其他控件)
void MainWindow::init()
{//视频文件
appPath=QCoreApplication::applicationDirPath();
model = new QFileSystemModel();
model->setRootPath(appPath);
videoPath=appPath+"/player";
ui->treeView_player->setModel(model);
ui->treeView_player->setRootIndex(model->index(videoPath));
m_mediaPlayer=new QMediaPlayer(ui->widget_player,QMediaPlayer::VideoSurface);
//视频播放
m_mediaPlayer=new QMediaPlayer(ui->widget_player,QMediaPlayer::VideoSurface);
videoWidget = new QVideoWidget;
videoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio);
playList=new QMediaPlaylist;
//播放器布局
QVBoxLayout *layout=new QVBoxLayout;
layout->addWidget(videoWidget);
ui->widget_player->setLayout(layout);
videoWidget->setPalette(Qt::black);
ui->playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
//滑块设置
ui->m_positionSlider->setRange(0, m_mediaPlayer->duration() / 1000);
connect(ui->m_positionSlider, &QSlider::sliderMoved, this, &MainWindow::seek);
ui->label_time->hide();
}
void MainWindow::initConnect() //连接slide和时间(快进 快退)
{
connect(m_mediaPlayer,SIGNAL(durationChanged(qint64)),this,SLOT(durationChanged(qint64)));
connect(m_mediaPlayer,SIGNAL(positionChanged(qint64)),this,SLOT(positionChanged(qint64)));
}
void MainWindow::on_playButton_clicked() //播放暂停按钮
{
if(m_mediaPlayer->state()==QMediaPlayer::PlayingState){
m_mediaPlayer->pause();
ui->playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); //播放时显示播放按钮 }
else {
m_mediaPlayer->play();
ui->playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause)); //暂停时显示播放按钮 }
}
void MainWindow::on_treeView_player_doubleClicked(const QModelIndex &index) //视频目录双击
{ //如果是文件夹 返回
if(model->isDir(index)){
return;
}
QString path=model->filePath(index);
playList->clear();//清空列表
playList->addMedia(QUrl::fromLocalFile(path));
m_mediaPlayer->setPlaylist(playList);
m_mediaPlayer->setVideoOutput(videoWidget);
m_mediaPlayer->play();
if(m_mediaPlayer->state()==QMediaPlayer::PlayingState){
ui->playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
ui->label_time->show(); }
}
void MainWindow::durationChanged(qint64 duration) //视频时间设置
{
m_duration = duration / 1000;
ui->m_positionSlider->setMaximum((int)duration/1000);
}
void MainWindow::positionChanged(qint64 progress) //滑块设置
{
//if (!m_positionSlider->isSliderDown())
ui-> m_positionSlider->setValue((int)progress / 1000);
updateDurationInfo(progress / 1000);
}
void MainWindow::updateDurationInfo(qint64 currentInfo) // slide时间显示格式设置
{
QString tStr;
if (currentInfo || m_duration) {
QTime currentTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60,
currentInfo % 60, (currentInfo * 1000) % 1000);
QTime totalTime((m_duration / 3600) % 60, (m_duration / 60) % 60,
m_duration % 60, (m_duration * 1000) % 1000);
QString format = "mm:ss";
if (m_duration > 3600)
format = "hh:mm:ss";
tStr = currentTime.toString(format) + " / " + totalTime.toString(format);
}
ui->label_time->setText(tStr);
}
void MainWindow::seek(int seconds) //滑块视频快进快退等
{
m_mediaPlayer->setPosition(seconds * 1000);
// m_mediaPlayer->play();
}
一些包含文件使用ctrl+enter可添加,或自己在帮助上查找添加等都可。
头文件或者pro文件有些地方可能不完善,方法还凑活能用。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133657.html原文链接:https://javaforall.cn