aplay播放声音
出处:https://www.jianshu.com/p/a96c15845ec9
#ifndef PLAYPCM_H
#define PLAYPCM_H
#include <QObject>
#include <QProcess>
class PlayPCM : public QObject
{
Q_OBJECT
QProcess process;
public:
explicit PlayPCM(int sampleRate = 44100, int channels = 2, int sampleSize = 16, QObject *parent = nullptr);
~PlayPCM();
signals:
public slots:
void writePCM(QByteArray array);
};
#endif // PLAYPCM_H
#include "playpcm.h"
#include <QDebug>
PlayPCM::PlayPCM(int sampleRate, int channels, int sampleSize, QObject *parent) : QObject(parent)
{
char cmd[128];
sprintf(cmd, "aplay -D hw:0,0 -r %d -c %d -f s%d", sampleRate, channels, sampleSize);
process.start(cmd);
if (process.waitForStarted()){
//qDebug() << "PCM player is ready.";
}
}
PlayPCM::~PlayPCM()
{
process.kill();
}
void PlayPCM::writePCM(QByteArray array)
{
if (process.isOpen())
process.write(array);
}

浙公网安备 33010602011771号