Qt - 播放PCM音频

PCM


     手头没有pcm音频可以使用ffmpeg将mp4切出pcm。

          ffmpeg -i xxx.mp4 -f s161e xxx.pcm

播放
#include <QAudioFormat>
#include <QThread>
#include <QAudioOutput>


void Pcm(const QString &str)
{
    
    
    QAudioFormat fmt;
    fmt.setSampleRate(44100);
    fmt.setSampleSize(16);
    fmt.setChannelCount(2);
    fmt.setCodec("audio/pcm");
    fmt.setByteOrder(QAudioFormat::LittleEndian);
    fmt.setSampleType(QAudioFormat::UnSignedInt);
    QAudioOutput *out = new QAudioOutput(fmt);
    QIODevice *io = out->start(); //开始播放

    int size = out->periodSize();
    char *buf = new char[size];

    FILE *fp = fopen(str.toStdString().c_str(), "rb");
    while (!feof(fp))
    {
    
    
        if (out->bytesFree() < size)
        {
    
    
            QThread::msleep(1);
            continue;
        }
        int len = fread(buf, 1, size, fp);
        if (len <= 0)break;
        io->write(buf, len);
    }
    fclose(fp);
    delete buf;
    buf = 0;
}

关注

微信公众号搜索"Qt_io_"或"Qt开发者中心"了解更多关于Qt、C++开发知识.。

笔者 - jxd

猜你喜欢

转载自blog.csdn.net/automoblie0/article/details/108111071
今日推荐