关于Qt下的alsa编程
本帖最后由 yiyaaixuexi 于 2012-12-21 17:03:30 编辑 现在我遇到的问题是从声卡那采集到的声音之后发送给接收方,但接收方没能正确的将声音播放出来。利用的是TCP来进行接收和发送的。求大侠指教。
附上发送和接收的主要代码:
接收方读取的相关代码:
void Widget::slotNewConnection()//当有一个新连接时触发的槽函数
{
qDebug()<<"new connection";
socket = server->nextPendingConnection();
connect(socket,SIGNAL(readyRead()),this,SLOT(slotReadyRead()));
}
void Widget::slotReadyRead()//有数据可取时触发的槽函数
{
qDebug()<<socket->read(buffer,size);
rc = snd_pcm_writei(handle,buffer,frames);
if(rc == -EPIPE)
{
qDebug()<<"underrun occured";
snd_pcm_prepare(handle);
}
else if(rc < 0)
{
qDebug()<<"error from writei"<<rc;
}
else if(rc != (int)frames)
{
qDebug()<<"short write"<<rc;
}
}
//对周期的大小和获取一定时间需要的周期数
frames = 32;
snd_pcm_hw_params_get_period_size(params,&frames,&dir);
size = frames * 4;
buffer = (char *)malloc(size);
snd_pcm_hw_params_get_period_time(params,&val,&dir);
loops = 5000000 / val;
void Widget::on_Button_start_clicked()
{
socket->connectToHost(QHostAddress("172.16.182.31"),PORT);
connect(socket,SIGNAL(connected()),this,SLOT(slotConnected()));
}
void Widget::slotConnected()//当两台机子连接在一起时触发的槽函数
{
qDebug()<<"connected";
init_device_capture();
while(loops > 0)
{
loops--;
qDebug()<<loops;
rc = snd_pcm_readi(handle,buffer,frames);
qDebug()<<rc;
if(rc == -EPIPE)
{
qDebug()<<"over run";
snd_pcm_prepare(handle);
}
else if(rc < 0)
qDebug()<<"error from read:"<<snd_strerror(rc);
else if(rc != (int)frames)
qDebug()<<"short read frames:"<<rc;
socket->write(buffer,size);
socket->waitForBytesWritten(-1);
}
qDebug()<<"end capture";
snd_pcm_drain(handle);
snd_pcm_close(handle);
}