Qt第十六章 多媒体Multimedia

文章目录

  • 多媒体
  • 音频
    • 播放音频
    • 录制音频
    • 低延迟音效
    • 低级音频播放和录制
    • 推送和拉取
    • 解码压缩音频到内存
    • 与音频处理相关的类
  • 视频
    • 播放视频
    • 处理低级视频帧
    • 录制视频
    • 与视频处理相关的类
  • 支持的媒体格式

多媒体

cmakelist 添加Multimedia模块

设备信息查询

#include 
#include 
#include 
#include 

void showDevs()
{
    QList<QAudioDevice> inAudios = QMediaDevices::audioInputs(); // 获取音频输入设备列表
    QList<QAudioDevice> outAudios = QMediaDevices::audioOutputs(); // 输出设备
    QList<QCameraDevice> cameras = QMediaDevices::videoInputs(); // 获取视频输入设备
    for (auto& ia : inAudios) {
        qInfo() << "设备名称:" << ia.description(); // 设备描述
        qInfo() << "设备Id:" << ia.id();
        qInfo() << "最大声道数:" << ia.maximumChannelCount(); // 最大声道数
        qInfo() << "最大采样率:" << ia.maximumSampleRate(); // 最大采样率

        QAudioFormat fmt = ia.preferredFormat();
        qInfo() << "=====默认设备参数====";
        qInfo() << "声道数" << fmt.channelCount();
        qInfo() << "声道配置" << fmt.channelConfig();
        qInfo() << "采样率" << fmt.sampleRate();
        qInfo() << "采样格式" << fmt.sampleFormat();
    }
    for (auto& ia : outAudios) {
        qInfo() << "设备名称:" << ia.description(); // 设备描述
        qInfo() << "设备Id:" << ia.id();
        qInfo() << "最大声道数:" << ia.maximumChannelCount(); // 最大声道数
        qInfo() << "最大采样率:" << ia.maximumSampleRate(); // 最大采样率

        QAudioFormat fmt = ia.preferredFormat();
        qInfo() << "=====默认设备参数====";
        qInfo() << "声道数" << fmt.channelCount();
        qInfo() << "声道配置" << fmt.channelConfig();
        qInfo() << "采样率" << fmt.sampleRate();
        qInfo() << "采样格式" << fmt.sampleFormat();
    }
    for (auto& cd : cameras) {

        qInfo() << "设备名称:" << cd.description(); // 设备描述
        qInfo() << "设备Id:" << cd.id();
        qInfo() << "设备方位" << cd.position();

        QList<QCameraFormat> fmt = cd.videoFormats();
        for (auto& f : fmt) {
            qInfo() << "最大帧率" << f.maxFrameRate();
            qInfo() << "最小帧率" << f.minFrameRate();
            qInfo() << "像素格式" << f.pixelFormat();
        }
    }
}

音频

cmakelist 添加MultimediaWidgets模块

播放音频

播放mp3、aac等非原始格式音频

void playAudio()
{
    QMediaPlayer* player = new QMediaPlayer;
    player->setSource(QUrl::fromLocalFile("E:/KwDownload/song/陈占美-恰似你的温柔.mp3"));
    QAudioOutput* adOut = new QAudioOutput;
    adOut->setVolume(0.8); // 设置音量,0到1之间
    player->setAudioOutput(adOut);
    player->play();
}

播放原始音频pcm格式

void playAudio2()
{
    QFile file("G:/qtproject/multiMedia/audio.pcm");
    file.open(QFile::ReadOnly);
    QAudioDevice dev = QMediaDevices::defaultAudioOutput();
    QAudioFormat fmt = dev.preferredFormat();

    QAudioSink* sink = new QAudioSink(dev, fmt);
    QIODevice* iodev = sink->start();
    while (true) {
        if (sink->bytesFree() > 85535)
            iodev->write(file.read(80000));
    }
    sink->start(iodev);
}

录制音频

存储位置只能是本地文件系统位置,存储格式也是固定的

void Widget::recordAudio()
{
    cs = new QMediaCaptureSession;
    // 输入设备
    audIn = new QAudioInput;
    audIn->setVolume(0.8);
    // 录制器
    recorder = new QMediaRecorder;

    cs->setAudioInput(audIn);
    cs->setRecorder(recorder);

    recorder->setQuality(QMediaRecorder::HighQuality);
    recorder->setOutputLocation(QUrl::fromLocalFile("C:/Users/PVer/Desktop/test")); // 设置存储路径
}

void Widget::on_pushButton_clicked()
{
    static bool state = true;
    if (state) {
        ui->pushButton->setText("结束录制");
        recorder->record();
    } else {
        ui->pushButton->setText("开始录制");
        recorder->stop();
    }
    state = !state;
}

低延迟音效

void testSoundEffect()
{
    QSoundEffect* ef = new QSoundEffect(QMediaDevices::defaultAudioOutput());
    ef->setSource(QUrl::fromLocalFile("../../currency.wav")); // 设置文件路径
    ef->setVolume(1);
    ef->setLoopCount(2);
    ef->play();
}

低级音频播放和录制

void recordAudio()
{
    QAudioFormat fmt;
    fmt.setChannelCount(2);
    fmt.setSampleFormat(QAudioFormat::Float);
    fmt.setSampleRate(44100);
    if (!QMediaDevices::defaultAudioInput().isFormatSupported(fmt)) {
        qCritical() << "不支持该格式";
        return;
    }

    QAudioSource* as = new QAudioSource(QMediaDevices::defaultAudioInput(), fmt);
    QFile* file = new QFile("../../test.pcm");
    if (!file->open(QFile::ReadWrite)) {
        qCritical() << "文件打开失败";
        return;
    }
    as->start(file);
    // 设置定时结束录制,否则原始音频太大
    QTimer::singleShot(3000, [=] { as->stop(); });
}

推送和拉取

通过QAudiSink类从QIODevice中拉去数据
相反数据可以写入QIODevice

解码压缩音频到内存

与音频处理相关的类

视频

播放视频

void playVideo()
{
    QMediaPlayer* player = new QMediaPlayer;
    player->setSource(QUrl::fromLocalFile("../../黑神话.mp4"));
    // 设置画面输出设备
    QVideoWidget* vw = new QVideoWidget;
    player->setVideoOutput(vw);
    vw->show();
    // 设置声音输出设备
    QAudioOutput* audio = new QAudioOutput;
    player->setAudioOutput(audio);
    // 开始播放
    player->play();
}

处理低级视频帧

录制视频

void Widget::recordVideo()
{
    cs2 = new QMediaCaptureSession;
    recorder2 = new QMediaRecorder;
    camera = new QCamera(QMediaDevices::defaultVideoInput());
    if (!camera->isAvailable()) {
        qInfo() << "没有可用摄像头";
        return;
    }
    QVideoWidget* vw = new QVideoWidget;

    cs2->setAudioInput(audIn);
    cs2->setRecorder(recorder2);
    cs2->setVideoOutput(vw);

    recorder2->setQuality(QMediaRecorder::HighQuality);
    recorder2->setOutputLocation(QUrl::fromLocalFile("C:/Users/PVer/Desktop/vediotest"));

    vw->show(); // 显示视频窗口
    camera->setActive(true); // 激活相机
}
//按钮的信号与槽连接
void Widget::on_pushButton_2_clicked()
{
    static bool state = true;
    if (state) {
        ui->pushButton_2->setText("结束录制");
        recorder2->record();
    } else {
        ui->pushButton_2->setText("开始录制");
        recorder2->stop();
    }
    state = !state;
}

与视频处理相关的类

支持的媒体格式

你可能感兴趣的:(qt,开发语言)