使用QT创建一个界面,可以通过按键实现按1 音乐暂停 按 2 音乐播放 按 3 单曲循环 按 4 列表循环 按 5 随机播放 按 6 上一首歌 按 7 下一首歌 按 0 退出播放器等功能,生成代码
时间: 2023-12-27 12:05:05 浏览: 138
以下是一个使用QT实现的简单音乐播放器界面的代码,包括上述功能:
```cpp
#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QFileDialog>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void on_btnPlayPause_clicked();
void on_btnStop_clicked();
void on_btnPrev_clicked();
void on_btnNext_clicked();
void on_btnLoop_clicked();
void on_btnShuffle_clicked();
void on_player_stateChanged(QMediaPlayer::State state);
void on_player_error(QMediaPlayer::Error error);
private:
Ui::MainWindow *ui;
QMediaPlayer *player;
QMediaPlaylist *playlist;
bool isPlaying;
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
player(new QMediaPlayer(this)),
playlist(new QMediaPlaylist(this)),
isPlaying(false)
{
ui->setupUi(this);
// Connect signals and slots
connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(on_player_stateChanged(QMediaPlayer::State)));
connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(on_player_error(QMediaPlayer::Error)));
// Set up playlist
playlist->setPlaybackMode(QMediaPlaylist::Sequential);
player->setPlaylist(playlist);
// Set up buttons
ui->btnPlayPause->setIcon(QIcon(":/icons/play.svg"));
ui->btnStop->setIcon(QIcon(":/icons/stop.svg"));
ui->btnLoop->setIcon(QIcon(":/icons/loop.svg"));
ui->btnShuffle->setIcon(QIcon(":/icons/shuffle.svg"));
}
MainWindow::~MainWindow()
{
delete ui;
delete player;
delete playlist;
}
void MainWindow::on_actionOpen_triggered()
{
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files"), "", tr("Audio Files (*.mp3 *.wav)"));
if (fileNames.isEmpty()) {
return;
}
// Add files to playlist
foreach (QString fileName, fileNames) {
playlist->addMedia(QUrl::fromLocalFile(fileName));
}
// Start playing if not already playing
if (!isPlaying) {
player->play();
ui->btnPlayPause->setIcon(QIcon(":/icons/pause.svg"));
isPlaying = true;
}
}
void MainWindow::on_btnPlayPause_clicked()
{
if (player->state() == QMediaPlayer::PlayingState) {
player->pause();
ui->btnPlayPause->setIcon(QIcon(":/icons/play.svg"));
isPlaying = false;
} else {
player->play();
ui->btnPlayPause->setIcon(QIcon(":/icons/pause.svg"));
isPlaying = true;
}
}
void MainWindow::on_btnStop_clicked()
{
player->stop();
ui->btnPlayPause->setIcon(QIcon(":/icons/play.svg"));
isPlaying = false;
}
void MainWindow::on_btnPrev_clicked()
{
playlist->previous();
}
void MainWindow::on_btnNext_clicked()
{
playlist->next();
}
void MainWindow::on_btnLoop_clicked()
{
QMediaPlaylist::PlaybackMode mode = playlist->playbackMode();
if (mode == QMediaPlaylist::Sequential) {
playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
ui->btnLoop->setIcon(QIcon(":/icons/loop-one.svg"));
} else {
playlist->setPlaybackMode(QMediaPlaylist::Sequential);
ui->btnLoop->setIcon(QIcon(":/icons/loop.svg"));
}
}
void MainWindow::on_btnShuffle_clicked()
{
playlist->setPlaybackMode(QMediaPlaylist::Random);
}
void MainWindow::on_player_stateChanged(QMediaPlayer::State state)
{
if (state == QMediaPlayer::StoppedState) {
ui->btnPlayPause->setIcon(QIcon(":/icons/play.svg"));
isPlaying = false;
}
}
void MainWindow::on_player_error(QMediaPlayer::Error error)
{
QMessageBox::critical(this, tr("Error"), player->errorString());
}
```
界面设计可以通过QT Designer进行,然后使用uic工具将.ui文件转换为对应的C++代码。
阅读全文