赞
踩
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
- #include <QMediaPlayer>
- #include <QVideoWidget>
- #include <QDir>
- #include <QListWidgetItem>
- namespace Ui {
- class Widget;
- }
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = nullptr);
- ~Widget();
- void updateDir(QString fileName);
-
- private slots:
- void OnMetaDataAvailableChanged(bool available);
-
- void OnPositionChanged(qint64 value);
-
- void OnStateChanged(QMediaPlayer::State value);
-
- void on_btnPlay_clicked();
-
- void on_btnPrev_clicked();
-
- void on_btnNext_clicked();
-
- void on_btnVolume_clicked();
-
- void on_btnOpenFile_clicked();
-
- void on_btnOpenDir_clicked();
-
- void on_listWidget_itemDoubleClicked(QListWidgetItem *item);
-
- void on_sldPlay_sliderMoved(int position);
-
- void on_sldVolume_sliderMoved(int position);
-
- private:
- Ui::Widget *ui;
- QMediaPlayer *mediaPlayer; //多媒体对象变量声明
- QVideoWidget *videoWidget; //视频窗口
- QDir *dir;//文件
- int index;
- int stoped;
- int prevVolume;
- };
-
- #endif // WIDGET_H
- #include "widget.h"
- #include "ui_widget.h"
- #include <QDebug>
- #include <QTime>
- #include <QFileDialog>
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- this->index = 0;
- this->stoped = 0;
- //实例化文件
- this->dir = new QDir();
- //实例化媒体
- this->mediaPlayer = new QMediaPlayer(this);
- // 实例化播放视频框框
- this->videoWidget = new QVideoWidget(this->ui->label);//设置视频父对象是标签
- this->videoWidget->resize(ui->label->size());//设置播放窗口大小
- // 将要播放的视频输出到播放视频框框中去
- this->mediaPlayer->setVideoOutput(this->videoWidget);
- this->ui->btnVolume->setText("");
- this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolume));
- this->ui->sldVolume->setRange(0, 100);
- this->ui->sldVolume->setValue(50);
- this->prevVolume = this->ui->sldVolume->value();
- this->mediaPlayer->setVolume(this->ui->sldVolume->value());//设置音量
- this->ui->sldPlay->setTracking(false);
- this->ui->sldVolume->setTracking(false);
- //信号链接槽函数
- //元数据改变
- connect(this->mediaPlayer, SIGNAL(metaDataAvailableChanged(bool)), this, SLOT(OnMetaDataAvailableChanged(bool)));
- //进度改变
- connect(this->mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(OnPositionChanged(qint64)));
- //状态改变
- connect(this->mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnStateChanged(QMediaPlayer::State)));
- //默认路径
- updateDir(R"(..\Video)");
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::updateDir(QString filePath)
- {
- //设置路径
- this->dir->setPath(filePath);
- this->ui->listWidget->clear();
- this->index = 0;
- QStringList namelist;
- namelist << "*.mp4";
- if (!this->dir->exists())
- {
- return;
- }
- this->dir->setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); //文件 不要.和..
- this->dir->setSorting(QDir::DirsFirst);//文件夹排在前面
- QFileInfoList list = this->dir->entryInfoList(namelist);
- for (int i = 0; i < list.size(); ++i)
- {
- QListWidgetItem *item = nullptr;
- if (list.at(i).isFile())
- {
- item = new QListWidgetItem(this->ui->listWidget);
- item->setText(list.at(i).fileName());
- item->setIcon(QIcon(this->style()->standardIcon(QStyle::SP_FileIcon)));//标准图标
- }
- if (item != nullptr)
- {
- this->ui->listWidget->addItem(item);
- }
- }
- if (this->ui->listWidget->count() > 0)
- {
- QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- }
- else
- {
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(" "));
- this->stoped = 0;
- this->ui->labVideoName->setText("未知");
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- }
- }
-
- void Widget::OnMetaDataAvailableChanged(bool available)
- {
- qDebug() << "OnMetaDataAvailableChanged";
- if (available)
- {
- qDebug() << "OnMetaDataAvailableChangedBool";
- int time = this->mediaPlayer->metaData("Duration").toInt();
- this->ui->labTotalTime->setText(QTime(0, 0, 0).addMSecs(time).toString("hh:mm:ss"));
- this->ui->sldPlay->setRange(0, time);//设置进度范围条
- }
- }
-
- void Widget::OnPositionChanged(qint64 value)
- {
- qDebug() << "OnPositionChanged";
- this->ui->labRealTime->setText(QTime(0, 0, 0).addMSecs(int(value)).toString("hh:mm:ss"));
- this->ui->sldPlay->setValue(int(value));
- }
- void Widget::OnStateChanged(QMediaPlayer::State value)
- {
- qDebug() << "OnStateChanged";
- if (QMediaPlayer::PlayingState == value)
- {
- qDebug() << "OnStateChanged播放";
- this->ui->btnPlay->setText("暂停");
- }
- else if (QMediaPlayer::PausedState == value)
- {
- qDebug() << "OnStateChanged暂停";
- this->ui->btnPlay->setText("播放");
- }
- else if (QMediaPlayer::StoppedState == value)
- {
- qDebug() << "OnStateChanged结束";
- qDebug() << this->stoped;
- this->ui->btnPlay->setText("播放");
- if (0 == this->stoped)
- {
- if ("顺序播放" == this->ui->cbxSelectMode->currentText())
- {
- qDebug() << "顺序播放";
- this->on_btnNext_clicked();
- }
- else if ("随机播放" == this->ui->cbxSelectMode->currentText())
- {
- qDebug() << "随机播放";
- if (ui->listWidget->count() != 1)
- {
- //设置随机数种子
- qsrand(uint(QTime(0, 0, 0).secsTo(QTime::currentTime())));
- while (1)
- {
- int rand = qrand() % this->ui->listWidget->count();
- if (rand != this->index)
- {
- this->index = rand;
- break;
- }
- }
- QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- }
- else
- {
- this->on_btnNext_clicked();
- }
- }
- else if ("循环播放" == this->ui->cbxSelectMode->currentText())
- {
- qDebug() << "循环播放";
- //啥也不做
- }
- this->mediaPlayer->play();
- }
- }
- }
- void Widget::on_btnPlay_clicked()
- {
- qDebug() << "on_btnPlay_clicked";
- if (this->ui->btnPlay->text() == "播放")
- {
- this->mediaPlayer->play();//开始播放
- }
- else if (this->ui->btnPlay->text() == "暂停")
- {
- this->mediaPlayer->pause();//暂停播放
- }
- }
-
- void Widget::on_btnPrev_clicked()
- {
- qDebug() << "on_btnPrev_clicked";
- //上一首
- --this->index;
- if (this->index < 0)
- {
- this->index = this->ui->listWidget->count() - 1;
- }
- //上一首名字
- QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
- //当前状态
- if (this->mediaPlayer->state() == QMediaPlayer::PlayingState)
- {
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- this->mediaPlayer->play();//保持播放状态
- return;
- }
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- }
-
- void Widget::on_btnNext_clicked()
- {
- qDebug() << "on_btnNext_clicked";
- //下一首
- ++this->index;
- if (this->index == this->ui->listWidget->count())
- {
- this->index = 0;
- }
- //下一首名字
- QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
- //当前状态
- if (this->mediaPlayer->state() == QMediaPlayer::PlayingState)
- {
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- this->mediaPlayer->play();//保持播放状态
- return;
- }
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- }
-
- void Widget::on_btnVolume_clicked()
- {
- qDebug() << "on_btnVolume_clicked";
- if (this->ui->sldVolume->value() != 0)
- {
- this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolumeMuted));
- this->ui->sldVolume->setValue(0);
- this->mediaPlayer->setVolume(0);
- }
- else
- {
- this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolume));
- this->ui->sldVolume->setValue(this->prevVolume);
- this->mediaPlayer->setVolume(this->prevVolume);
- }
- }
-
- void Widget::on_btnOpenFile_clicked()
- {
- qDebug() << "on_btnOpenDir_clicked";
- QString arg("MP4 (*.mp4)");
- QString fileNamePath = QFileDialog::getOpenFileName(this, "选择歌曲", R"(..\Music)", "MP4 (*.mp4)", &arg);
- if (fileNamePath == nullptr)
- {
- return;
- }
- this->ui->listWidget->clear();
- this->index = 0;
- QFileInfo fileInfo(fileNamePath);
- QListWidgetItem *item = new QListWidgetItem(this->ui->listWidget);
- item->setText(fileInfo.fileName());
- this->dir->setPath(R"(..\Music)");
- item->setIcon(QIcon(this->style()->standardIcon(QStyle::SP_FileIcon)));//标准图标
- if (item != nullptr)
- {
- this->ui->listWidget->addItem(item);
- }
- if (this->ui->listWidget->count() > 0)
- {
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileNamePath));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- }
- else
- {
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(" "));
- this->stoped = 0;
- this->ui->labVideoName->setText("未知");
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- }
- }
-
- void Widget::on_btnOpenDir_clicked()
- {
- qDebug() << "on_btnOpenDir_clicked";
- QString tmpPath = QFileDialog::getExistingDirectory();
- if (tmpPath == nullptr)
- {
- return;
- }
- updateDir(tmpPath);
- }
-
- void Widget::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
- {
- qDebug() << "on_listWidget_itemDoubleClicked";
- for (int i = 0; i < this->ui->listWidget->count(); ++i)
- {
- if (item->text() == this->ui->listWidget->item(i)->text())
- {
- this->index = i;
- }
- }
- //选定的名字
- QString fileName = this->dir->absolutePath() + R"(/)" + this->ui->listWidget->item(this->index)->text();
- //当前状态
- if (this->mediaPlayer->state() == QMediaPlayer::PlayingState)
- {
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- this->mediaPlayer->play();//保持播放状态
- return;
- }
- this->stoped = 1;
- this->mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
- this->stoped = 0;
- this->ui->labVideoName->setText(this->ui->listWidget->item(this->index)->text());
- this->setWindowTitle(this->ui->labVideoName->text() + " - Qt视频播放器");
- }
-
- void Widget::on_sldPlay_sliderMoved(int position)
- {
- qDebug() << "on_sldReal_sliderMoved";
- this->mediaPlayer->setPosition(position);//设置位置
- }
-
- void Widget::on_sldVolume_sliderMoved(int position)
- {
- qDebug() << "on_sldVolume_sliderMoved";
- this->prevVolume = position;
- this->mediaPlayer->setVolume(position);//设置音量
- if (0 == position)
- {
- this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolumeMuted));
- }
- else
- {
- this->ui->btnVolume->setIcon(this->style()->standardIcon(QStyle::SP_MediaVolume));
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。