Qt中,视频播放的功能主要是通过 QMediaPlayer类和 QVideoWidget类来实现。在使用这两个类时,需要在 .pro项目配置文件中添加对应的模块——multimedia 和 multimediawidgets。
核心的API如下:
下面来模拟实现音频文件的播放。
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// 实例化对象
QSound* sound = new QSound(":/di.wav", this); // 设置添加的音频文件di.wav
connect(ui->pushButton, &QPushButton::clicked, this, [=](){
sound->play(); //播放
});
}
视频的播放与此同理,这里不做说明。