Qt播放器常用设置
?
播放效果?
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文件有些地方可能不完善,方法還湊活能用。
?
總結
- 上一篇: 前端学习(1852)vue之电商管理系统
- 下一篇: “睡服”面试官系列第二十二篇之class