PCM音频播放QT2

        在实际项目中,用到更多的是Qt播放音频数据流。我测试过使用CSDN的定时器版本代码,会出现一些问题。

QaudioOutput有两种模式,前面播放文件采用的是pull mode,这里采用push mode。Push模式可以采用定时器把数据写给QIODevice,这里还是使用MyDevice类来进行数据写入。

功能:循环顺序播放三个音频文件

流程:

首先播放第一个文件,第一个文件播放完成播放第二个文件,第二个文件播放完成后,播放第三个文件,第三个文件播放完成后播放第一个文件。使用两个定时器来进行流程控制:其中一个定时器负责写入音频到QIODevice另外一个定时器负责判断文件是否播放完成。

具体做法:

  1. 类头文文件
#ifndef MYDEVICE_H
#define MYDEVICE_H
#include <QIODevice>
class mydevice : public QIODevice
{
public:
    QByteArray data_pcm; //存放pcm数据
    int        len_written; //记录已写入多少字节
public:
    mydevice(); //创建对象传递pcm数据
    ~mydevice();
    qint64 readData(char *data, qint64 maxlen); //重新实现的虚函数
    qint64 writeData(const char *data, qint64 len); //它是个纯虚函数, 不得不实现
};
#endif // MYDEVICE_H

2、类

#include "mydevice.h"
#include <QDebug>
mydevice::mydevice()
{
    this->open(QIODevice::ReadOnly); // 为了解决QIODevice::read (QIODevice): device not open
    len_written = 0;
}
mydevice::~mydevice()
{
    this->close();
}
qint64 mydevice::readData(char *data, qint64 maxlen) // data为声卡的数据缓冲区地址, maxlen为声卡缓冲区最大能存放的字节数
{
    if (len_written >= data_pcm.size())
        return 0;
    int len;
    //计算未播放的数据的长度
    len = (len_written+maxlen) > data_pcm.size() ? (data_pcm.size() - len_written) : maxlen;
    memcpy(data, data_pcm.data()+len_written, len); //把要播放的pcm数据存入声卡缓冲区里
    len_written += len; //更新已播放的数据长度
    return len;
}
qint64 mydevice::writeData(const char *data, qint64 len)
{
    return len;
}
  1. 调用函数
  1. 定时器1播放单独文件,send_done==1表示读完成
void MainWindow::slotTimeout()
{
    QByteArray buffer(32768, 0);
    if(send_len<fileLength)
    {
    if(audioOutput&&audioOutput->state()!=QAudio::StoppedState&&audioOutput->state()!=QAudio::SuspendedState)
        {
            int chunks = audioOutput->bytesFree() / audioOutput->periodSize();
            while (chunks) {
               const qint64 len = dev->readData(buffer.data(), audioOutput->periodSize());
               if (len)
               {
                   streamOut->write(buffer.data(), len);
                   send_len = send_len+len;
               }
               if (len != audioOutput->periodSize())
               {
                    send_done =1;
                    break;
               }
               --chunks;
            }
        }
    }
}
  1. 定时器二,切换文件
void MainWindow::slotTimeout2()
{
    if(send_done==1)
    {
        send_len =0;
        dev->len_written=0;
        send_done=0;
        if(i == 0)
            {
                i=1;
                dev->data_pcm =ba2;
                fileLength = dev->data_pcm.size();
            }
        else if (i == 1) {

                i=2;
                dev->data_pcm =ba1;
                fileLength = dev->data_pcm.size();
        }
        else {
            i=0;
            dev->data_pcm =ba3;
            fileLength = dev->data_pcm.size();
        }
    }
}
  1. 设置音频格式和读文件
void MainWindow::on_pushButton_clicked()
{
    QString audio_filename;
    audio_filename = QFileDialog::getOpenFileName(this,"请选择音频文件","../");
    if(audio_filename.isNull())
    {
        return;
    }
    ui->lineEdit->setText(audio_filename);

    QFile f(audio_filename);
    if (!f.open(QIODevice::ReadOnly))
       exit(0);
    ba1= f.readAll();
    f.close();
    dev->data_pcm =ba1;
    fileLength = dev->data_pcm.size();
    qDebug()<<"文件长度"<<fileLength;

    QString audio_filename2="C:/Users/lyc/Desktop/pcm2.pcm";
    QFile f2(audio_filename2);
    if (!f2.open(QIODevice::ReadOnly))
        exit(0);
    ba2 = f2.readAll();
    f2.close();

    QString audio_filename3="C:/Users/lyc/Desktop/pcm3.pcm";
    QFile f3(audio_filename3);
    if (!f3.open(QIODevice::ReadOnly))
        exit(0);
    ba3 = f3.readAll();
    f3.close();

    send_done =0;
    QAudioFormat audioFormat;
    audioFormat.setSampleRate(44100);
    audioFormat.setChannelCount(1);
    audioFormat.setSampleSize(16);
    audioFormat.setCodec("audio/pcm");
    audioFormat.setByteOrder(QAudioFormat::LittleEndian);
    audioFormat.setSampleType(QAudioFormat::SignedInt);
    QAudioDeviceInfo info = QAudioDeviceInfo::defaultOutputDevice();
    if (!info.isFormatSupported(audioFormat)) {
        qDebug()<<"default format not supported try to use nearest";
        audioFormat = info.nearestFormat(audioFormat);
    }
    audioOutput  = new QAudioOutput(audioFormat, this);
    streamOut = audioOutput->start();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电子厂的吕小春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值