播放pcm 音频

#include <QCoreApplication>
#include<QAudioFormat>
#include<QAudioOutput>
#include<QIODevice>
#include<QThread>
#include <QFile>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QAudioFormat fmt;
    fmt.setSampleRate(44100); //采样率
    fmt.setSampleSize(16);  // 32->16  音频重采样
    fmt.setChannelCount(2); //通道数 2
    fmt.setCodec("audio/pcm"); //通用格式
    fmt.setByteOrder(QAudioFormat::LittleEndian);//小端
    fmt.setSampleType(QAudioFormat::SignedInt);


    QAudioOutput* out = new QAudioOutput(fmt);
    QIODevice* io = out->start(); 

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

    FILE * fp = fopen("D:/desktop/out.pcm","rb");
    if(!fp)
    {
        printf("open failed");
        return -1;
    }

    while (!feof(fp)) {
        if(out->bytesFree() < size)
        {
            QThread::msleep(1);
            continue;
        }
        int len = fread(buffer, 1, size, fp);
        if(len <= 0)
        {
            break;
        }
        io->write(buffer, len);
    }

    fclose(fp);
    delete[] buffer;
    return a.exec();
}

全部评论

相关推荐

11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务