下一个能播放视频和音乐并且功能基本齐全的Qt代码
时间: 2023-06-11 12:09:02 浏览: 53
以下是一个基本的Qt代码,可以播放本地视频和音乐文件,包括基本的控制功能:
```cpp
#include <QtWidgets>
#include <QtMultimedia>
class VideoPlayer : public QWidget
{
Q_OBJECT
public:
VideoPlayer(QWidget *parent = nullptr);
private slots:
void openFile();
void play();
void pause();
void stop();
void updatePosition(qint64 position);
void setPosition(int position);
private:
QMediaPlayer *player;
QLabel *titleLabel;
QSlider *positionSlider;
QPushButton *playButton;
QPushButton *pauseButton;
QPushButton *stopButton;
};
VideoPlayer::VideoPlayer(QWidget *parent)
: QWidget(parent), player(new QMediaPlayer(this))
{
// UI setup
titleLabel = new QLabel(tr("No media file loaded."));
positionSlider = new QSlider(Qt::Horizontal);
positionSlider->setRange(0, player->duration() / 1000);
playButton = new QPushButton(tr("Play"));
connect(playButton, &QPushButton::clicked, this, &VideoPlayer::play);
pauseButton = new QPushButton(tr("Pause"));
connect(pauseButton, &QPushButton::clicked, this, &VideoPlayer::pause);
stopButton = new QPushButton(tr("Stop"));
connect(stopButton, &QPushButton::clicked, this, &VideoPlayer::stop);
QPushButton *openButton = new QPushButton(tr("Open"));
connect(openButton, &QPushButton::clicked, this, &VideoPlayer::openFile);
// Layout setup
QGridLayout *layout = new QGridLayout;
layout->addWidget(titleLabel, 0, 0, 1, 4);
layout->addWidget(positionSlider, 1, 0, 1, 4);
layout->addWidget(playButton, 2, 0);
layout->addWidget(pauseButton, 2, 1);
layout->addWidget(stopButton, 2, 2);
layout->addWidget(openButton, 2, 3);
setLayout(layout);
// Player setup
connect(player, &QMediaPlayer::positionChanged, this, &VideoPlayer::updatePosition);
connect(player, &QMediaPlayer::durationChanged, [this](qint64 duration) {
positionSlider->setRange(0, duration / 1000);
});
}
void VideoPlayer::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"));
if (!fileName.isEmpty()) {
player->setMedia(QUrl::fromLocalFile(fileName));
titleLabel->setText(QFileInfo(fileName).fileName());
playButton->setEnabled(true);
}
}
void VideoPlayer::play()
{
player->play();
pauseButton->setEnabled(true);
stopButton->setEnabled(true);
}
void VideoPlayer::pause()
{
player->pause();
}
void VideoPlayer::stop()
{
player->stop();
pauseButton->setEnabled(false);
stopButton->setEnabled(false);
}
void VideoPlayer::updatePosition(qint64 position)
{
positionSlider->setValue(position / 1000);
}
void VideoPlayer::setPosition(int position)
{
player->setPosition(position * 1000);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
VideoPlayer player;
player.show();
return app.exec();
}
#include "main.moc"
```
要使用Qt Multimedia模块,需要在.pro文件中添加以下行:
```
QT += multimedia
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)