qt 播放pcm音频

一、获取PCM音频

ffmpeg -i input.mp3 -acodec pcm_s16le -ar 44100 -ac 2 -f s16le output.pcm

  • -acodec pcm_s16le:指定16位小端PCM编码格式(兼容性最佳)
  • -ar 44100:设置采样率为CD标准44.1kHz(可替换为16000/8000等)‌
  • -ac 2:保留立体声(单声道用 -ac 1)‌
  • -f s16le:强制输出二进制裸数据格式‌

二、播放

方式1:

复制代码
int main(int argc, char *argv[])
{
    qSetMessagePattern("[GUI]%{file}(%{line}): %{message}");
    QGuiApplication app(argc, argv);

    QFile file(argv[1]);    // 需要播放的音频
    if (file.open(QIODevice::ReadOnly))
    {
        qDebug() << "打开成功!";
        QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
        qDebug() << info.supportedSampleTypes();   // 输出支持的样本类型列表。
        qDebug() << info.supportedByteOrders();    // 输出支持的字节顺序列表
        qDebug() << info.supportedCodecs();        // 输出可用编码器
        qDebug() << info.supportedSampleRates();   // 输出支持的采样率

        QAudioFormat fmt;
        fmt.setSampleRate(44100);   // 设置采样率
        fmt.setSampleSize(16);      // 设置样本大小
        fmt.setChannelCount(2);     // 设置使用双通道
        fmt.setCodec("audio/pcm");  // 设置编解码器
        fmt.setByteOrder(QAudioFormat::LittleEndian);   // 使用小端
        fmt.setSampleType(QAudioFormat::SignedInt);   //使用无符号整数样本类型(如果在linux下显示支持UnSignedInt,但是实际打开会失败,改用SignedInt就可以了)

        if (!info.isFormatSupported(fmt))
        {
            qDebug() << "输出设备不支持该格式,不能播放音频";
            return 0;
        }

        QAudioOutput* audio = new QAudioOutput(fmt, qApp);
        audio->setVolume(QString(argv[2]).toFloat());
        audio->start(&file);
    }

    return app.exec();
}

方式2:

复制代码
int main(int argc, char *argv[])
{
    qSetMessagePattern("[GUI]%{file}(%{line}): %{message}");
    QGuiApplication app(argc, argv);

    QFile file(argv[1]);    // 需要播放的音频
    if (file.open(QIODevice::ReadOnly))
    {
        qDebug() << "打开成功!";
        QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
        qDebug() << info.supportedSampleTypes();   // 输出支持的样本类型列表。
        qDebug() << info.supportedByteOrders();    // 输出支持的字节顺序列表
        qDebug() << info.supportedCodecs();        // 输出可用编码器
        qDebug() << info.supportedSampleRates();   // 输出支持的采样率

        QAudioFormat fmt;
        fmt.setSampleRate(44100);   // 设置采样率
        fmt.setSampleSize(16);      // 设置样本大小
        fmt.setChannelCount(2);     // 设置使用双通道
        fmt.setCodec("audio/pcm");  // 设置编解码器
        fmt.setByteOrder(QAudioFormat::LittleEndian);   // 使用小端
        fmt.setSampleType(QAudioFormat::SignedInt);   //使用无符号整数样本类型(如果在linux下显示支持UnSignedInt,但是实际打开会失败,改用SignedInt就可以了)

        if (!info.isFormatSupported(fmt))
        {
            qDebug() << "输出设备不支持该格式,不能播放音频";
            return 0;
        }

        QAudioOutput* audio = new QAudioOutput(fmt, qApp);  
        audio->setVolume(QString(argv[2]).toFloat());
        QIODevice* io = audio->start();
        int size = audio->periodSize();     // 这是每个周期防止缓冲区欠载和确保不间断播放所需的数据量。
        QByteArray buf = file.readAll();    // 将需要播放的音频数据读到buf中
        file.close();

        while (!buf.isEmpty())
        {
            if (audio->bytesFree() < size)   // 音频缓冲区中可用的空闲字节数,判断缓冲区是否可写入数据。
            {
                QThread::msleep(1);
                continue;
            }

            io->write(buf.mid(0, size));   // 写入需要播放的数据
            buf.remove(0, size);
            //qDebug() << buf.count();
        }

        io->close();
        delete audio;
        audio = nullptr;
    }

    return app.exec();
}

觉得有帮助的话,打赏一下呗。。

需要商务合作(定制程序)的欢迎私信!!

相关推荐
the3clipse7 小时前
Windows 11 26H2 声音与音频问题排查指南:没声音、麦克风不收音与蓝牙音频优化
音视频·le audio·windows 11 26h2·win11没声音·麦克风没声音·蓝牙耳机音质·音频服务修复
开开心心就好11 小时前
PDF水印怎么去掉?勾选对象再点匹配
java·前端·mysql·支持向量机·pdf·音视频·模拟退火算法
RobinDevNotes1 天前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
伯奥克客服1 天前
2026年视频综合一体机架构选型:从接口协议到流媒体底层的深度技术解析
架构·音视频
开开心心就好1 天前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
FansAI_art2 天前
多角色 AIGC 短片的双模型分工:静态生图与图生视频怎么接
aigc·音视频
山顶夕景3 天前
【ECCV2026】流式视频理解Thinking in Streaming Video
音视频·vlm·视频理解·多模态理解·流式视频
zhanlang02174 天前
HTML 零基础入门到企业实战【第五章 5.4】内嵌网页 iframe 标签:嵌入网页、视频、地图实战
前端·html·音视频
TengTaiTech4 天前
头戴耳机做运动相机外接麦:一颗麦克风同时服务蓝牙通话与 3.5mm 录音的底层重构
数码相机·音视频·蓝牙·qcc
2301_790355975 天前
【技术拆解】短视频配音自动化流程:从文案预处理到 SRT 字幕的 9 个环节
自然语言处理·音视频