基于qt的音乐播放器源码
时间: 2023-10-06 10:07:04 浏览: 183
以下是一个基于Qt的简单音乐播放器示例代码:
mainwindow.h文件:
```c++
#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 = nullptr);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void on_actionPlay_triggered();
void on_actionStop_triggered();
void on_actionPause_triggered();
void on_actionNext_triggered();
void on_actionPrevious_triggered();
void on_actionMute_triggered();
void on_actionVolumeUp_triggered();
void on_actionVolumeDown_triggered();
void on_positionChanged(qint64 position);
void on_durationChanged(qint64 duration);
void on_volumeChanged(int volume);
private:
Ui::MainWindow *ui;
QMediaPlayer *m_player;
QMediaPlaylist *m_playlist;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp文件:
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_player = new QMediaPlayer(this);
m_playlist = new QMediaPlaylist(this);
m_player->setPlaylist(m_playlist);
connect(m_player, SIGNAL(positionChanged(qint64)), this, SLOT(on_positionChanged(qint64)));
connect(m_player, SIGNAL(durationChanged(qint64)), this, SLOT(on_durationChanged(qint64)));
connect(m_player, SIGNAL(volumeChanged(int)), this, SLOT(on_volumeChanged(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(), tr("Audio Files (*.mp3 *.wav)"));
if (!fileName.isEmpty()) {
m_playlist->addMedia(QUrl::fromLocalFile(fileName));
m_player->setPlaylist(m_playlist);
ui->statusBar->showMessage(tr("File loaded: %1").arg(fileName));
}
}
void MainWindow::on_actionPlay_triggered()
{
m_player->play();
}
void MainWindow::on_actionStop_triggered()
{
m_player->stop();
}
void MainWindow::on_actionPause_triggered()
{
m_player->pause();
}
void MainWindow::on_actionNext_triggered()
{
m_playlist->next();
}
void MainWindow::on_actionPrevious_triggered()
{
m_playlist->previous();
}
void MainWindow::on_actionMute_triggered()
{
m_player->setMuted(!m_player->isMuted());
}
void MainWindow::on_actionVolumeUp_triggered()
{
int volume = m_player->volume() + 10;
if (volume > 100) {
volume = 100;
}
m_player->setVolume(volume);
}
void MainWindow::on_actionVolumeDown_triggered()
{
int volume = m_player->volume() - 10;
if (volume < 0) {
volume = 0;
}
m_player->setVolume(volume);
}
void MainWindow::on_positionChanged(qint64 position)
{
ui->positionSlider->setValue(position);
}
void MainWindow::on_durationChanged(qint64 duration)
{
ui->positionSlider->setRange(0, duration);
}
void MainWindow::on_volumeChanged(int volume)
{
ui->volumeSlider->setValue(volume);
}
```
这个示例代码中包含了一个QMediaPlayer和一个QMediaPlaylist,可以播放MP3和WAV格式的音频文件。它包含了一些基本的功能,如打开、播放、暂停、停止、上一曲、下一曲、静音、音量调节等。你可以根据需要进行修改和扩展。
阅读全文