实现思路
- 多媒体模块功能概述:
- QT 的多媒体模块提供了丰富的功能,包括音频播放、录制、视频播放和摄像头操作等。
- 播放音频:
- 使用
QMediaPlayer
播放完整的音频文件。 - 使用
QSoundEffect
播放简短的音效文件。
- 录制音频:
- 使用
QMediaRecorder
类进行音频录制。 - 使用
QAudioSource
和 QAudioSink
类采集和播放原始音频数据。
- 播放视频文件:
- 在
QVideoWidget
或 QGraphicsVideoItem
上播放视频。
- 摄像头的使用:
代码示例
1. 基于 QMediaPlayer 的音乐播放器
#include <QtWidgets/QApplication>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtMultimedia/QMediaPlaylist>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QUrl>
#include <QtCore/QDebug>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QMediaPlayer *player = new QMediaPlayer();QMediaPlaylist *playlist = new QMediaPlaylist();playlist->addMedia(QUrl::fromLocalFile("path/to/your/audio.mp3"));playlist->setPlaybackMode(QMediaPlaylist::Loop);player->setPlaylist(playlist);QPushButton *playButton = new QPushButton("Play");connect(playButton, &QPushButton::clicked, player, &QMediaPlayer::play);layout->addWidget(playButton);mainWidget->show();return app.exec();
}
2. 使用 QSoundEffect 播放音效文件
#include <QtWidgets/QApplication>
#include <QtMultimedia/QSoundEffect>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QSoundEffect *effect = new QSoundEffect();effect->setSource(QUrl::fromLocalFile("path/to/your/sound.wav"));QPushButton *playButton = new QPushButton("Play Sound");connect(playButton, &QPushButton::clicked, effect, &QSoundEffect::play);layout->addWidget(playButton);mainWidget->show();return app.exec();
}
3. 录制音频
#include <QtWidgets/QApplication>
#include <QtMultimedia/QMediaRecorder>
#include <QtMultimedia/QAudioRecorder>
#include <QtMultimedia/QAudioEncoderSettings>
#include <QtMultimedia/QVideoEncoderSettings>
#include <QtMultimedia/QCamera>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QUrl>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QMediaRecorder *recorder = new QMediaRecorder();QAudioEncoderSettings audioSettings;audioSettings.setCodec("audio/mp3");audioSettings.setQuality(QMultimedia::HighQuality);recorder->setAudioSettings(audioSettings);QPushButton *recordButton = new QPushButton("Record");connect(recordButton, &QPushButton::clicked, [recorder]() {if (recorder->state() == QMediaRecorder::StoppedState) {recorder->record();} else {recorder->stop();}});layout->addWidget(recordButton);mainWidget->show();return app.exec();
}
4. 采集和播放原始音频数据
#include <QtWidgets/QApplication>
#include <QtMultimedia/QAudioSource>
#include <QtMultimedia/QAudioSink>
#include <QtMultimedia/QAudioFormat>
#include <QtMultimedia/QAudioDeviceInfo>
#include <QtCore/QIODevice>
#include <QtCore/QDebug>class AudioProcessor : public QIODevice {Q_OBJECT
public:AudioProcessor(QObject *parent = nullptr) : QIODevice(parent) {}qint64 readData(char *data, qint64 maxlen) override {return 0;}qint64 writeData(const char *data, qint64 len) override {qDebug() << "Received audio data of length:" << len;return len;}
};int main(int argc, char *argv[]) {QApplication app(argc, argv);QAudioFormat format;format.setSampleRate(44100);format.setChannelCount(2);format.setSampleSize(16);format.setCodec("audio/pcm");format.setByteOrder(QAudioFormat::LittleEndian);format.setSampleType(QAudioFormat::UnSignedInt);QAudioDeviceInfo inputDevice = QAudioDeviceInfo::defaultInputDevice();if (!inputDevice.isFormatSupported(format)) {qWarning() << "Default input device does not support the format";format = inputDevice.nearestFormat(format);}QAudioSource *audioSource = new QAudioSource(format);AudioProcessor *processor = new AudioProcessor();audioSource->start(processor);QAudioDeviceInfo outputDevice = QAudioDeviceInfo::defaultOutputDevice();QAudioSink *audioSink = new QAudioSink(outputDevice, format);audioSink->start(processor);return app.exec();
}#include "main.moc"
5. 在 QVideoWidget 上播放视频文件
#include <QtWidgets/QApplication>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QUrl>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QMediaPlayer *player = new QMediaPlayer();QVideoWidget *videoWidget = new QVideoWidget();player->setVideoOutput(videoWidget);player->setMedia(QUrl::fromLocalFile("path/to/your/video.mp4"));QPushButton *playButton = new QPushButton("Play Video");connect(playButton, &QPushButton::clicked, player, &QMediaPlayer::play);layout->addWidget(playButton);layout->addWidget(videoWidget);mainWidget->show();return app.exec();
}
6. 在 QGraphicsVideoItem 上播放视频文件
#include <QtWidgets/QApplication>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimediaWidgets/QGraphicsVideoItem>
#include <QtWidgets/QGraphicsView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QUrl>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QMediaPlayer *player = new QMediaPlayer();QGraphicsVideoItem *videoItem = new QGraphicsVideoItem();player->setVideoOutput(videoItem);player->setMedia(QUrl::fromLocalFile("path/to/your/video.mp4"));QGraphicsScene *scene = new QGraphicsScene();scene->addItem(videoItem);QGraphicsView *view = new QGraphicsView(scene);QPushButton *playButton = new QPushButton("Play Video");connect(playButton, &QPushButton::clicked, player, &QMediaPlayer::play);layout->addWidget(playButton);layout->addWidget(view);mainWidget->show();return app.exec();
}
7. 摄像头的使用(拍照和录像)
#include <QtWidgets/QApplication>
#include <QtMultimedia/QCamera>
#include <QtMultimedia/QCameraViewfinder>
#include <QtMultimedia/QCameraImageCapture>
#include <QtMultimedia/QMediaRecorder>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget *mainWidget = new QWidget();QVBoxLayout *layout = new QVBoxLayout(mainWidget);QCamera *camera = new QCamera(QCamera::defaultCamera());QCameraViewfinder *viewfinder = new QCameraViewfinder();camera->setViewfinder(viewfinder);QCameraImageCapture *imageCapture = new QCameraImageCapture(camera);QMediaRecorder *mediaRecorder = new QMediaRecorder(camera);QPushButton *captureButton = new QPushButton("Take Photo");connect(captureButton, &QPushButton::clicked, [imageCapture]() {imageCapture->capture();});QPushButton *recordButton = new QPushButton("Start/Stop Recording");connect(recordButton, &QPushButton::clicked, [mediaRecorder]() {if (mediaRecorder->state() == QMediaRecorder::StoppedState) {mediaRecorder->record();} else {mediaRecorder->stop();}});layout->addWidget(viewfinder);layout->addWidget(captureButton);layout->addWidget(recordButton);camera->start();mainWidget->show();return app.exec();
}
代码解释
1. 基于 QMediaPlayer 的音乐播放器
- QMediaPlayer:
QMediaPlayer
用于播放音频和视频文件。QMediaPlaylist
用于管理播放列表,可设置播放模式,如循环、顺序、随机等。
2. 使用 QSoundEffect 播放音效文件
- QSoundEffect:
- 适合播放短音效,使用
setSource
加载音效文件,点击按钮调用 play
播放。
3. 录制音频
- QMediaRecorder:
QMediaRecorder
可以录制音频和视频。QAudioEncoderSettings
可设置音频编码和质量。
4. 采集和播放原始音频数据
- QAudioSource:
- QAudioSink:
- AudioProcessor:
- 自定义
QIODevice
子类,重写 writeData
处理接收到的音频数据。
5. 在 QVideoWidget 上播放视频文件
- QMediaPlayer:
- 用于播放视频,通过
setVideoOutput
关联到 QVideoWidget
进行播放。
6. 在 QGraphicsVideoItem 上播放视频文件
- QMediaPlayer:
- 关联到
QGraphicsVideoItem
,添加到 QGraphicsScene
并通过 QGraphicsView
显示。
7. 摄像头的使用(拍照和录像)
- QCamera:
- 表示摄像头设备,使用
setViewfinder
关联视图。
- QCameraImageCapture:
- QMediaRecorder:
- 用于录像,调用
record
和 stop
控制录制过程。
使用说明
- 对于每个示例,将代码保存为
main.cpp
文件。 - 确保
.pro
文件包含 QT += multimedia multimediawidgets widgets
以及 CONFIG += c++11
。 - 编译并运行程序,观察多媒体功能的效果。