基于qt的影音娱乐系统代码
时间: 2023-08-25 08:04:23 浏览: 116
以下是一个基于Qt的简单影音娱乐系统的代码示例,包含了播放器、视频列表和音频列表等功能:
mainwindow.h:
```
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void on_playButton_clicked();
void on_pauseButton_clicked();
void on_stopButton_clicked();
void on_previousButton_clicked();
void on_nextButton_clicked();
void on_videoList_doubleClicked(const QModelIndex &index);
void on_audioList_doubleClicked(const QModelIndex &index);
private:
Ui::MainWindow *ui;
QMediaPlayer *player;
QMediaPlaylist *playlist;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
playlist = new QMediaPlaylist(player);
//设置播放模式为循环播放
playlist->setPlaybackMode(QMediaPlaylist::Loop);
//将视频列表和音频列表设置为播放列表
player->setPlaylist(playlist);
//设置视频列表和音频列表为多选模式
ui->videoList->setSelectionMode(QAbstractItemView::MultiSelection);
ui->audioList->setSelectionMode(QAbstractItemView::MultiSelection);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QStringList fileNames = QFileDialog::getOpenFileNames(this,
tr("Open File"), "",
tr("Video Files (*.mp4 *.avi);;Audio Files (*.mp3 *.wav)"));
if(fileNames.isEmpty())
return;
//将选中的文件添加到播放列表中
for(int i = 0; i < fileNames.size(); i++)
{
playlist->addMedia(QUrl::fromLocalFile(fileNames.at(i)));
}
//将选中的文件添加到视频列表或音频列表中
for(int i = 0; i < fileNames.size(); i++)
{
QFileInfo fileInfo(fileNames.at(i));
if(fileInfo.suffix() == "mp4" || fileInfo.suffix() == "avi")
{
ui->videoList->addItem(fileInfo.fileName());
}
else if(fileInfo.suffix() == "mp3" || fileInfo.suffix() == "wav")
{
ui->audioList->addItem(fileInfo.fileName());
}
}
//如果没有正在播放,则播放第一个文件
if(player->state() != QMediaPlayer::PlayingState)
{
player->play();
}
}
void MainWindow::on_playButton_clicked()
{
player->play();
}
void MainWindow::on_pauseButton_clicked()
{
player->pause();
}
void MainWindow::on_stopButton_clicked()
{
player->stop();
}
void MainWindow::on_previousButton_clicked()
{
playlist->previous();
}
void MainWindow::on_nextButton_clicked()
{
playlist->next();
}
void MainWindow::on_videoList_doubleClicked(const QModelIndex &index)
{
//根据选中的视频文件索引,设置播放列表中的当前媒体
playlist->setCurrentIndex(index.row());
//开始播放
player->play();
}
void MainWindow::on_audioList_doubleClicked(const QModelIndex &index)
{
//根据选中的音频文件索引,设置播放列表中的当前媒体
playlist->setCurrentIndex(ui->videoList->count() + index.row());
//开始播放
player->play();
}
```
此代码仅为示例,实际应用中可以根据需要进行修改和优化。
阅读全文