Qt5使用Qt Multimedia
模块来实现多媒体应用
多媒体功能以及相关的C++类
要使用多媒体模块,必须再工程文件中添加模块:
QT += multimedia
音频播放
Qt对底层做了很好的封装。实现音频播放十分简单。
常用方法
-
设置媒体源
设置播放哪个视频
[slot] void QMediaPlayer::setMedia(const QMediaContent &media, QIODevice *stream = Q_NULLPTR)
- 获取声音以及获取声音大小
int volume() const
void setVolume(int volume)
- 获取播放时长
qint64 duration() const //单位ms
- 获取以及播放位置
qint64 position() const //单位ms
void setPosition(qint64 position)
- 播放、暂停、停止
[slot] void QMediaPlayer::play()
[slot] void QMediaPlayer::pause() //暂停播放,保留上次播放位置
[slot] void QMediaPlayer::stop() //停止播放 再次播放会从头开始
- 设置音量大小
void setVolume(int volume)
常用信号
- 音量改变信号
[signal] void QMediaPlayer::volumeChanged(int volume)
- 播放位置改变的信号
[signal] void QMediaPlayer::positionChanged(qint64 position)
- 播放时长改变信号
[signal] void QMediaPlayer::positionChanged(qint64 position)
- 音量改变信号
[signal] void QMediaPlayer::volumeChanged(int volume)
实现简单音乐播放
播放界面:
代码实现:
class MyMusicPlayer : public QMainWindow
{
Q_OBJECT
public:
explicit MyMusicPlayer(QWidget *parent = 0);
~MyMusicPlayer();
private:
Ui::MyMusicPlayer *ui;
QMediaPlayer *musicPlayer;
QString musicName;
QSlider *volSlider; //调节、显示音乐播放进度
public slots:
void openMusicFile(void);
void playPauseMusic(void);
void setSliderRange(void); //根据音乐不同时长设置滑动条范围
void setMusicTime(void);
};
#include "mymusicplayer.h"
#include "ui_mymusicplayer.h"
#include <QMediaPlayer>
#include <QToolButton>
#include <QFileDialog>
#include <QDebug>
MyMusicPlayer::MyMusicPlayer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyMusicPlayer)
{
ui->setupUi(this);
setWindowTitle(tr("音乐播放器v1.0"));
musicPlayer = new QMediaPlayer; //创建一个播放器
ui->playStopPushButton->setEnabled(false); //没有打开音频文件,先禁用
ui->musicHorizontalSlider->setEnabled(false);
ui->playStopPushButton->setToolTip(tr("播放")); //设置鼠标悬停提示
ui->volToolButton_2->setToolTip(tr("音量调节"));
ui->OpenFileToolButton->setToolTip(tr("打开音乐"));
ui->playStopPushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause)); //添加暂停图标
ui->volToolButton_2->setIcon(style()->standardIcon(QStyle::SP_MediaVolume)); //添加音量图标
connect(ui->OpenFileToolButton, &QToolButton::clicked, this, &MyMusicPlayer::openMusicFile); //打开音乐
connect(ui->playStopPushButton, &QPushButton::clicked, this, &MyMusicPlayer::playPauseMusic); //播放、暂停音乐
connect(this->musicPlayer, &QMediaPlayer::positionChanged, ui->musicHorizontalSlider, &QSlider::setValue);//播放位置改变,改变滑动条位置
connect(this->musicPlayer, &QMediaPlayer::durationChanged, this, &MyMusicPlayer::setSliderRange); //根据时长设置滑动条范围
connect(this->musicPlayer, &QMediaPlayer::positionChanged, this, &MyMusicPlayer::setMusicTime);
connect(this->musicPlayer, &QMediaPlayer::durationChanged, this, &MyMusicPlayer::setMusicTime);
connect(ui->musicHorizontalSlider, &QSlider::valueChanged, musicPlayer, &QMediaPlayer::setPosition); //滑动改变,改变播放位置
// connect(ui->volToolButton_2, &QToolButton::clicked, this, &MyMusicPlayer::createVolAdjustWidget);
ui->volToolButton_2->setPopupMode(QToolButton::InstantPopup); //设置toolButton点击弹出,而不触发按钮的点击动作
}
MyMusicPlayer::~MyMusicPlayer()
{
delete ui;
}
//播放音乐
void MyMusicPlayer::playPauseMusic(void)
{
switch (musicPlayer->state()) //根据播放状态执行不同的动作
{
case QMediaPlayer::StoppedState: //停止
ui->playStopPushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));//设置播放图标
musicPlayer->play(); //播放音乐
break;
case QMediaPlayer::PlayingState: //播放
ui->playStopPushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));//设置暂停图标
musicPlayer->pause(); //暂停音乐
break;
case QMediaPlayer::PausedState: //暂停
ui->playStopPushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));//设置播放图标
musicPlayer->play(); //播放音乐
default:
break;
}
}
void MyMusicPlayer::openMusicFile(void)
{
musicName = QFileDialog::getOpenFileName(this, tr("打开音频文件"), "D:", tr("音频文件(*mp3 *wav)"));
QString tmpMusicName = musicName;
qDebug() << musicName;
if (!tmpMusicName.isEmpty())
{
int index = tmpMusicName.lastIndexOf('/'); //找到最后一个'/'的位置
tmpMusicName.remove(0, index+1); //找到音乐名字
ui->musicNameLabel->setText(tmpMusicName); //显示音乐名字
ui->playStopPushButton->setEnabled(true); //使能播放暂停按钮
ui->musicHorizontalSlider->setEnabled(true);
musicPlayer->setMedia(QUrl::fromLocalFile(musicName)); //设置媒体源
qDebug() << "duration : " << musicPlayer->duration();
qDebug() << "position :" << musicPlayer->position();
qDebug() << tmpMusicName; //调试打印音乐文件名
}
}
void MyMusicPlayer::setSliderRange(void)
{
qint64 nduration = musicPlayer->duration(); //获取歌曲时长
qDebug() << "slider duration:" << nduration;
ui->musicHorizontalSlider->setRange(0, (int)nduration); //设置进度条范围
}
void MyMusicPlayer::setMusicTime(void)
{
qint64 musicDuration = musicPlayer->duration(); //获取歌曲时长
QString musicTime = QString("%1:%2").arg(musicDuration/1000/60).arg(musicDuration/1000%60);
ui->musicTimeLabel->setText(musicTime); //显示歌曲时间
qDebug() << "duration : " << musicDuration;
qDebug() << "position :" << musicPlayer->position();
}
音量调节还未实现。
视频播放
QMediaPlayer
也可以播放视频文件,但只有声音,没有图像。
要实现图像显示需要借助的类:QVideoWidget
或QGraphicsVideoItem
QVideoWidget
除了可以显示播放视频画面,还可以设置视频的亮度、对比度、饱和度等。这些功能都有相应的信号和槽。
GIF
格式图片使用QMovie
播放。
实现视频播放需要的模块:
QT += multimedia multimediawidgets
常用方法
QMediaPlayer方法:
- 设置视频输的窗口
void QMediaPlayer::setVideoOutput(QVideoWidget *output)
或
void QMediaPlayer::setVideoOutput(QGraphicsVideoItem *output)
QVideoWidget的方法:
- 设置、获取亮度
[slot] void setBrightness(int brightness)
int brightness() const
- 设置、获取饱和度
void setSaturation(int saturation)
int saturation() const
- 设置、获取对比度
void setContrast(int contrast)
int contrast() const
- 设置、获取色调
void setHue(int hue)
int hue() const
- 设置全屏显示
void setFullScreen(bool fullScreen)
常用信号
- 亮度改变
void brightnessChanged(int brightness)
- 对比度改变
void contrastChanged(int contrast)
- 饱和度改变
void saturationChanged(int saturation)
- 色调改变
void hueChanged(int hue)
实现简单视频播放
#ifndef EASYVIDEOPLAYER_H
#define EASYVIDEOPLAYER_H
#include <QWidget>
#include <QMediaPlayer>
#include <QVideoWidget>
namespace Ui {
class EasyVideoPlayer;
}
class EasyVideoPlayer : public QWidget
{
Q_OBJECT
public:
explicit EasyVideoPlayer(QWidget *parent = 0);
~EasyVideoPlayer();
public slots:
void openVideo(void);
private:
Ui::EasyVideoPlayer *ui;
QMediaPlayer *videoPlayer;
QVideoWidget *videoWidget;
QString videoName;
};
#endif // EASYVIDEOPLAYER_H
#include "easyvideoplayer.h"
#include "ui_easyvideoplayer.h"
#include <QVideoWidget>
#include <QMediaPlayer>
#include <QFileDialog>
EasyVideoPlayer::EasyVideoPlayer(QWidget *parent) :
QWidget(parent),
ui(new Ui::EasyVideoPlayer)
{
ui->setupUi(this);
videoPlayer = new QMediaPlayer(this);
videoWidget = new QVideoWidget(this);
videoWidget->resize(600, 400);
videoWidget->move((this->width()-600)/2, 30);
videoWidget->show();
videoPlayer->setVideoOutput(videoWidget);
ui->brightnessSlider->setRange(0, 100);
ui->brightnessSlider->setValue(videoWidget->brightness());
connect(ui->brightnessSlider, &QSlider::valueChanged, this->videoWidget, &QVideoWidget::setBrightness);
connect(this->videoWidget, &QVideoWidget::brightnessChanged, ui->brightnessSlider, &QSlider::setValue);
ui->contrastSlider->setRange(0, 100);
ui->contrastSlider->setValue(videoWidget->contrast());
connect(ui->contrastSlider, &QSlider::valueChanged, this->videoWidget, &QVideoWidget::setContrast);
connect(this->videoWidget, &QVideoWidget::contrastChanged, ui->contrastSlider, &QSlider::setValue);
ui->hueSlider->setRange(0, 100);
ui->hueSlider->setValue(videoWidget->hue());
connect(ui->hueSlider, &QSlider::valueChanged, this->videoWidget, &QVideoWidget::setHue);
connect(this->videoWidget, &QVideoWidget::hueChanged, ui->hueSlider, &QSlider::setValue);
ui->saturationSlider->setRange(0, 100);
ui->saturationSlider->setValue(videoWidget->saturation());
connect(ui->saturationSlider, &QSlider::valueChanged, this->videoWidget, &QVideoWidget::setSaturation);
connect(this->videoWidget, &QVideoWidget::saturationChanged, ui->saturationSlider, &QSlider::setValue);
connect(ui->openVideoButton, &QToolButton::clicked, this, &EasyVideoPlayer::openVideo);
}
EasyVideoPlayer::~EasyVideoPlayer()
{
delete ui;
}
void EasyVideoPlayer::openVideo(void)
{
videoName = QFileDialog::getOpenFileName(this, tr("打开视频文件"), "D:", tr("视频(*mp4 *mkv *wmv)"));
qDebug() << "video name :" << videoName;
videoPlayer->setMedia(QUrl::fromLocalFile(videoName)); //设置播放源
videoPlayer->play(); //播放
}