公司项目有个背景音乐的功能,所以就做了个简版播放器
使用QMediaPlayer,QMediaPlaylist,QVideoWidget我这主要就是循环播放
音量条和进度条还不知道做不做,先留着吧,等做再完善
新增进度条控制,点击事件,还有关闭窗口音乐播放的bug
audioWindow.h
#ifndef AUDIOWINDOW_H
#define AUDIOWINDOW_H
#include <QDialog>
#include <QWidget>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
#include <QTimer>
#include <QSlider>
#include <QMouseEvent>
#include <QCoreApplication>
class QDialog;
class CustomSlider : public QSlider
{
Q_OBJECT
public:
CustomSlider(QWidget *parent = 0) : QSlider(parent)
{
}
protected:
void mousePressEvent(QMouseEvent *ev);//重写QSlider的mousePressEvent事件
signals:
void costomSliderClicked();//自定义的鼠标单击信号,用于捕获并处理
};
class AudioWindow :public QDialog
{
Q_OBJECT
public:
AudioWindow(QWidget* parent = 0);
void closeEvent(QCloseEvent *event);
private:
void initLayout();
QString path;
QMediaPlayer* player;
//与Slider有关的播放控制变量
QTimer * timer;
int maxValue = 1000;//设置进度条的最大值
CustomSlider* sliderDuration;
QVideoWidget* videoWidget;
QMediaPlaylist* playlist;
private slots:
void closeAudio();
void openAudio();
void createAudioPlayer();
void restartAudio();
void stopAudio();
void pauseAudio();
void playAudio();
void sliderProgressClicked();
void sliderProgressMoved();
void sliderProgressReleased();
void onTimerOut();
};
#endif // !AUDIOWINDOW_H
audioWindow.cpp
#include "audioWindow.h"
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMessageBox>
#include <QSlider>
AudioWindow::AudioWindow(QWidget *parent)
: QDialog(parent)
{
initLayout();
}
void AudioWindow::initLayout()
{
this->setWindowTitle(QStringLiteral("背景音乐"));
this->setObjectName(QStringLiteral("AudioWindow"));;
QHBoxLayout* hlayout1 = new QHBoxLayout();
QHBoxLayout* hlayout2 = new QHBoxLayout();
QVBoxLayout* vlayout1 = new QVBoxLayout();
QPushButton* openAudioFile = new QPushButton(QStringLiteral("打开背景音乐"));
QPushButton* playAudio = new QPushButton(QStringLiteral("播放"));
QPushButton* pauseAudio = new QPushButton(QStringLiteral("暂停"));
QPushButton* stopAudio = new QPushButton(QStringLiteral("停止"));
QPushButton* continueAudio = new QPushButton(QStringLiteral("继续"));
QPushButton* closeButton = new QPushButton(QStringLiteral("关闭"));
sliderDuration = new CustomSlider();
sliderDuration->setOrientation(Qt::Horizontal);
//读取文件后再启用
sliderDuration->setEnabled(false);
QObject::connect(openAudioFile, SIGNAL(clicked()), this, SLOT(openAudio()));
QObject::connect(playAudio, SIGNAL(clicked()), this, SLOT(playAudio()));
QObject::connect(pauseAudio, SIGNAL(clicked()), this, SLOT(pauseAudio()));
QObject::connect(stopAudio, SIGNAL(clicked()), this, SLOT(stopAudio()));
QObject::connect(continueAudio, SIGNAL(clicked()), this, SLOT(restartAudio()));
QObject::connect(closeButton, SIGNAL(clicked()), this, SLOT(closeAudio()));
//进度条控制
QObject::connect(sliderDuration, SIGNAL(costomSliderClicked()), this, SLOT(sliderProgressClicked()));
QObject::connect(sliderDuration, SIGNAL(sliderMoved()), this, SLOT(sliderProgressMoved()));
QObject::connect(sliderDuration, SIGNAL(sliderReleased()), this, SLOT(sliderProgressReleased()));
hlayout1->addWidget(sliderDuration);
hlayout2->addWidget(openAudioFile);
hlayout2->addWidget(playAudio);
hlayout2->addWidget(pauseAudio);
hlayout2->addWidget(continueAudio);
hlayout2->addWidget(closeButton);
vlayout1->addLayout(hlayout1);
vlayout1->addLayout(hlayout2);
this->setLayout(vlayout1);
player = new QMediaPlayer();
videoWidget = new QVideoWidget();
playlist = new QMediaPlaylist();
}
void AudioWindow::closeAudio() //关闭播放器
{
if (player != NULL){
player->stop();
}
this->close(); //关闭播放器
}
void AudioWindow::playAudio() //播放
{
player->play();
}
/**
* 音频插入动作
*/
void AudioWindow::openAudio()
{
QString fileName = QFileDialog::getOpenFileName(this, QStringLiteral("打开音频文件"), QDir::currentPath()); //获取文件打开路径
if (fileName.isEmpty()) {
QMessageBox::warning(this, "Warning", QStringLiteral("未选择打开文件"));
return;
};
//启用slider并设置范围
sliderDuration->setEnabled(true);
sliderDuration->setRange(0, maxValue);
timer = new QTimer();
timer->setInterval(1000);
timer->start();
//将timer连接至onTimerOut槽函数
connect(timer, SIGNAL(timeout()), this, SLOT(onTimerOut()));
path = fileName;
createAudioPlayer();
}
/**
* 创建音频播放器
*/
void AudioWindow::createAudioPlayer()
{
playlist->addMedia(QUrl(path));
playlist->setCurrentIndex(0);
playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
player->setPlaylist(playlist);
player->setVideoOutput(videoWidget);
videoWidget->show();
player->setVolume(60);
}
/**
* 音频继续播放动作
*/
void AudioWindow::restartAudio()
{
if (player == NULL)
{
return;
}
player->play();
}
/**
* 音频停止播放动作
*/
void AudioWindow::stopAudio()
{
if (player == NULL)
{
return;
}
player->stop();
}
/**
* 音频暂停播放动作
*/
void AudioWindow::pauseAudio()
{
//pauseFlag = true;
//time.stop();
if (player == NULL)
{
return;
}
player->pause();
}
void AudioWindow::onTimerOut()
{
if (player == NULL)
{
return;
}
sliderDuration->setValue(player->position()*maxValue / player->duration());
}
//单击、拖动、释放
void AudioWindow::sliderProgressClicked()
{
if (player == NULL)
{
return;
}
player->setPosition(sliderDuration->value()*player->duration() / maxValue);
}
void AudioWindow::sliderProgressMoved()
{
//暂时停止计时器,在用户拖动过程中不修改slider的值
if (player == NULL)
{
return;
}
timer->stop();
player->setPosition(sliderDuration->value()*player->duration() / maxValue);
}
void AudioWindow::sliderProgressReleased()
{
//用户释放滑块后,重启定时器
if (timer == NULL)
{
return;
}
timer->start();
}
void CustomSlider::mousePressEvent(QMouseEvent *ev)
{
//注意应先调用父类的鼠标点击处理事件,这样可以不影响拖动的情况
QSlider::mousePressEvent(ev);
//获取鼠标的位置,这里并不能直接从ev中取值(因为如果是拖动的话,鼠标开始点击的位置没有意义了)
double pos = ev->pos().x() / (double)width();
setValue(pos * (maximum() - minimum()) + minimum());
//发送自定义的鼠标单击信号
emit costomSliderClicked();
}
void AudioWindow::closeEvent(QCloseEvent *event)
{
if (player != NULL)
{
player->stop();
}
event->accept(); //接受退出信号,程序退出
}