QIODevice+QAudioInput+QAudioOutput播放实时音频

liningzheng 2014-12-31 12:30:43
初学QT编程,了解不是很深,现在做了这样一个功能,遇到了些麻烦,还希望大家能伸出援手,谢谢!
线程1使用QIODevice+QAudioInput获取当前录音,音频输入Device为WriteOnly,当写数据到设备中时把该buffer通过网络发送到本地线程2进行接收。
线程2使用QIODevice+QAudioOutput,当获得到线程1发送过来的数据时,把该buffer写入到音频输出Device,该设备也是WriteOnly,通过对比线程1发送的数据和线程2得到的一致,QIODevice写入数据时也返回了成功,但就是不能播放出对应的音频。
线程1是实时录入和发送,线程2也是实时收取,不存在误差。

发送端:
void realTimeRadio::initInputDevice()
{
//todo;发送给界面进行更新
audioformat.setFrequency(8000);//设置录入音频的format
audioformat.setChannels(1);
audioformat.setSampleSize(16);
audioformat.setSampleType(QAudioFormat::SignedInt);
audioformat.setByteOrder(QAudioFormat::LittleEndian);
audioformat.setCodec("audio/pcm");

QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
if (!info.isFormatSupported(audioformat)) {
audioformat = info.nearestFormat(audioformat);
emit msgNotify("Default format not supported - trying to use nearest");
}
//AudioInfo继承自QIODevice,重写了writedata
audioInfo = new AudioInfo(audioformat, this);
//writedata函数中发送update(const char*,qint64),OnUpdate进行网络传输
connect(audioInfo, SIGNAL(update(const char*,qint64)), this, SLOT(OnUpdate(const char*,qint64)));

createAudioInput();
}

void realTimeRadio::createAudioInput()
{
audioInput = new QAudioInput(audioDevice, audioformat, this);
audioInfo->start();
audioInput->start(audioInfo);
}
接收端:
void listenManager::initializeAudio()
{
//设置QAudioOutput的format,和发送端保持一致
audioformat.setFrequency(8000);
audioformat.setChannels(1);
audioformat.setSampleSize(16);
audioformat.setCodec("audio/pcm");
audioformat.setByteOrder(QAudioFormat::LittleEndian);
audioformat.setSampleType(QAudioFormat::SignedInt);

QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(audioformat)) {
audioformat = info.nearestFormat(audioformat);
}
//audioOutputInfo继承QIODevice,ReadData和WriteData里面均为空,只返回数据长度
outputInfo = new audioOutputInfo(audioformat);
createAudioOutput();
}
void listenManager::createAudioOutput()
{
audioOutput = new QAudioOutput(audiodevice, audioformat, this);
outputInfo->start();
audioOutput->start(outputInfo);
//recSession获取线程1的数据,然后发送radioNotify消息,outputInfo接收,然后在OnradioNotify写入数据,且成功
connect(&recSession, SIGNAL(radioNotify(const char*)), outputInfo, SLOT(OnradioNotify(const char*)));
}

audioOutputInfo类:
qint64 audioOutputInfo::readData(char *data, qint64 len)
{
return len;
}
qint64 audioOutputInfo::writeData(const char *data, qint64 len)
{
return len;
}
qint64 audioOutputInfo::bytesAvailable() const
{
int ilen = QIODevice::bytesAvailable();
return QIODevice::bytesAvailable();
}
void audioOutputInfo::OnradioNotify(const char* data)
{
audioMutex.lock();
int ret = write(data);
audioMutex.unlock();
}
...全文
1836 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunRainPenguin110 2015-05-20
  • 打赏
  • 举报
回复
我也遇到了这样的问题,并且我是初学Qt的,我把数据直接写入QIODevice的话,会出现很大杂声,但是我把数据保存为WAV文件,并用其他播放器是能够播放的。你后来这个问题解决了吗,或者说你有找到过相关的播放网络传输过来的音频的例子吗?跪谢
liningzheng 2014-12-31
  • 打赏
  • 举报
回复
没有进行保存,音频输入到Device中时得到其内容,随即进行网络传输,另外一个线程得到该数据,然后写入到QAudioOutput的Device中,数据是实时更新的,保存也没有意义,如果是保存到本地然后再读取就不是我要实现的功能了。 有意思的一点是我继承了QIODevice,重写readData和writeData(内部未对数据进行操作,只返回得到的数据长度),这样输出时一直是连续的蜂鸣声,如果是用QIODevice本身,那么就会得到声音信息,但是杂声很大,声音的辨识度不高。
shenchenman 2014-12-31
  • 打赏
  • 举报
回复
你保存了么?

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧