当前位置:编程学习 > wap >>

qt2录音功能求助

qt2中如何调用/dev/dsp  并将录音文件保存在U盘上 求高手帮忙=。=万分感谢 --------------------编程问答-------------------- qt2?我有木有看错? --------------------编程问答--------------------
引用 1 楼  的回复:
qt2?我有木有看错?

木有看错...具体点就是QT2.2.0
高手求指点 --------------------编程问答-------------------- 如果我木有看错的话,我只能说,这样的高手木有了……

不过,我认为这与Qt关系不大了,你把Linux本地代码混编进来就可以了。 --------------------编程问答--------------------
引用 3 楼  的回复:
如果我木有看错的话,我只能说,这样的高手木有了……

不过,我认为这与Qt关系不大了,你把Linux本地代码混编进来就可以了。

那你能帮我看看代码嘛  编译通过了 但是在ubuntu下的qt里跑不起来 不知道问题出哪了...


mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include "qstring.h"
#include "qsound.h"
#include <qapplication.h>
#include <qprogressbar.h>
#include <qpushbutton.h>
#include <qlistbox.h>
#include <qslider.h>
#include <qwidget.h>
#include <qcolor.h>
#include <qdatetime.h>
#include <qthread.h> 
#include <qevent.h>
#include <qsound.h>
#include <qfont.h>
#include <qpixmap.h>
#include <qtimer.h>
#include <qtextstream.h> 
#include <qstringlist.h>
#include <qfile.h>
#include <qlabel.h>
#include <sys/soundcard.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/soundcard.h>

#define LENGTH 30 //存储秒数
#define RATE 22050 //采样频率
#define SIZE 8 //量化位数
#define CHANNELS 1 //声道数目



class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget( QWidget *parent=0, const char *name=0 );
void init();

signals:
void clicked();             
public slots:
void lRecord(); //录音
void lStop(); //停止
void rStop();               //录音停止
void pStop();               //播放停止
void lPlay(); //播放
void lClean(); //清除
void lVolUp();              //调整声音(大)
void lVolDown();            //调整声音(小)
void lBar(); //进度条
void lQuit(); //退出
void lAffirm(); //确定
void rAffirm(); //准备
 
private:
QPushButton *Record;        //录音按钮
QPushButton *Play;          //播放按钮
QPushButton *Stop;          //停止按钮
QPushButton *Clean;         //清除按钮
QPushButton *Quit;          //退出按钮
QPushButton *Affirm;        //确定按钮
QPushButton *VolUp;         //音量按钮(大)
QPushButton *VolDown;       //音量按钮(小)
QListBox *ListBox; //存储框
QLabel *label0; //存储文件标题
QLabel *label1; //音量条与进度条底板
QLabel *label2; //进度条
QLabel *label3; //音量条
QLabel *label4;
QLabel *label5; //音量条(高)
QLabel *label6; //进度条(开始)
QLabel *label7; //标题
QLabel *label8; //存储框阴影
QLabel *label9; //信息灯
QFile *Remove; //文件删除
QSound *sound;
QStringList lines; //行数
QString line;


QTime time1; //计时器
QTimer *timer; //时间
int i; //循环变量
int iTime[5]; //存储时间的数组(5个)
int iState; //状态
int iVol; //音量
int iIndex; //循环
int iBar; //初始化进度条长度
int iPlBar; //录完音播放时的进度条
char cStr[30];
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8]; //录制的声音所占的字节
int pcmfd;


};

#endif


main.cpp

#include <qapplication.h>
#include "mywidget.h"

int main( int argc, char **argv )
{
    QApplication a( argc, argv );

    MyWidget w;
    w.setGeometry( 200, 150, 640, 480 );
    a.setMainWidget( &w );
    w.show();
    return a.exec();
};


--------------------编程问答-------------------- mywidget.cpp
#include "mywidget.h"
#include <qapplication.h>
#include <qwidget.h>
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8]; //录制的声音所占的字节
MyWidget::MyWidget( QWidget *parent, const char *name ): QWidget( parent, name,Qt::WStyle_Customize | Qt::WStyle_NoBorder)
{
setMaximumSize(640,480);
setMinimumSize(640,480);
setPalette(QPalette(QColor(255,255,157)));

Record = new QPushButton( "Record", this, "Record" ); //录音
Record->setGeometry( 50, 420, 60, 40 );
Record->setFont( QFont( "Times", 10, QFont::Bold ) );
QPixmap pix_Record("./pic/record1.bmp");
Record->setPixmap(pix_Record);
connect( Record, SIGNAL(clicked()), this, SLOT(lRecord()) );

Play = new QPushButton( "Play", this, "Play" ); //播放
Play->setGeometry( 150, 420, 60, 40 );
Play->setFont( QFont( "Times", 10, QFont::Bold ) );
QPixmap pix_Play("./pic/play1.bmp");
Play->setPixmap(pix_Play);
connect( Play, SIGNAL(clicked()), this, SLOT(lPlay()) );

Stop = new QPushButton( "Stop", this, "Stop" ); //停止
Stop->setGeometry( 250, 420, 60, 40 );
Stop->setFont( QFont( "Times", 10, QFont::Bold));
QPixmap pix_Stop("./pic/stop1.bmp");
Stop->setPixmap(pix_Stop);
connect( Stop, SIGNAL(clicked()), this, SLOT(lStop()) );

Clean = new QPushButton( "Clean", this, "Clean" ); //消除
Clean->setGeometry( 350, 420, 60, 40 );
Clean->setFont( QFont( "Times", 10, QFont::Bold));
QPixmap pix_Clean("./pic/del1.bmp");
Clean->setPixmap(pix_Clean);
connect( Clean, SIGNAL(clicked()), this, SLOT(lClean()) );

label0 = new QLabel(this); //存储文件标题
label0->setGeometry( 150, 140, 140, 20);
label0->setBackgroundColor( QColor( 255, 255, 255) );
QPixmap pix_label0("./pic/box.bmp");
label0->setPixmap(pix_label0);

label1 = new QLabel(this); //音量条与进度条底板
label1->setGeometry( 50, 78, 540, 36);
label1->setBackgroundColor( QColor( 255, 196, 15) );

label2 = new QLabel(this); //进度条
label2->setGeometry( 95, 88, 300, 16);
label2->setBackgroundColor( QColor( 255, 255, 255) );

label3 = new QLabel(this); //音量条
label3->setGeometry( 470, 88, 80, 16);
label3->setBackgroundColor( QColor( 255, 255, 255) );

label4 = new QLabel(this);
label4->setGeometry( 95, 88, 0, 16);

label5 = new QLabel(this); //音量条(高)
iVol=40;
label5->setGeometry( 470, 88, 40, 16);
label5->setBackgroundColor( QColor( 255, 0, 0) );

label6 = new QLabel(this); //进度条(开始)
label6->setGeometry( 95, 88, 0, 16);

label7 = new QLabel(this); //标题
label7->setGeometry( 10, 10, 600, 40);
label7->setBackgroundColor( QColor( 253, 230, 72) );
QPixmap pix_label6("./pic/voice memo.bmp");
label7->setPixmap(pix_label6);
label7->setFont( QFont( "Times", 18, QFont::Bold ) );

label9=new QLabel(this); //信息灯
label9->setGeometry( 600, 440, 40, 40);
QPixmap pix_label9("./pic/deng.bmp");
label9->setPixmap(pix_label9);

Quit = new QPushButton( "Quit", this, "Quit" ); //退出
Quit->setGeometry( 550, 20, 40, 20 );
Quit->setFont( QFont( "Times", 10, QFont::Bold ) );
Quit->setPalette(QPalette(QColor(253,187,47)));
connect( Quit, SIGNAL(clicked()), this, SLOT(lQuit()) );

Affirm = new QPushButton( "Start", this, "Affirm" ); //确定
Affirm->setGeometry( 500, 20, 40, 20 );
Affirm->setFont( QFont( "Times", 10, QFont::Bold ) );
Affirm->setPalette(QPalette(QColor(253,187,47)));
connect( Affirm, SIGNAL(clicked()), this, SLOT(lAffirm()) );

VolUp = new QPushButton( "+", this, "VolUp" ); //音量(大)按钮
VolUp->setGeometry( 560, 88, 20, 20 );
VolUp->setFont( QFont( "Times", 10, QFont::Bold ));
QPixmap pix_VolUp("./pic/volup.bmp");
VolUp->setPixmap(pix_VolUp);
connect( VolUp, SIGNAL(clicked()), this, SLOT(lVolUp()) );

VolDown = new QPushButton( "-", this, "VolDown" ); //音量(小)按钮
VolDown->setGeometry( 440, 88, 20, 20);
VolDown->setFont( QFont( "Times", 10, QFont::Bold ) );
QPixmap pix_VolDown("./pic/voldown.bmp");
VolDown->setPixmap(pix_VolDown);
connect( VolDown, SIGNAL(clicked()), this, SLOT(lVolDown()) );
iState=0; //不录音也不播放
label8=new QLabel(this);
label8->setGeometry( 155, 165, 350, 230); //存储框的阴影
label8->setBackgroundColor( QColor( 128, 128, 128) );
ListBox=new QListBox(this); //存储框
ListBox->setGeometry( 150, 160, 350, 230);
for(i=0;i<5;i++)
{
ListBox->insertItem("");
}

iState=0; //平时状态0(1为录音状态,2为播放状态)
ListBox=new QListBox(this);
ListBox->setGeometry(150,160,350,230);
for(i=0;i<5;i++)
{
ListBox->insertItem("");
}

    QFile file( "file.txt" ); //创建存储文件
    if ( file.open( IO_ReadOnly ) ) 
{
        QTextStream stream( &file );
        int n = 0;
        while ( !stream.eof() ) 
{
            line = stream.readLine(); 
ListBox->changeItem(line,n++);
        }
        file.close();
    }

    QFile filetime( "time.txt" ); //创建存储声音时间
if ( filetime.open( IO_ReadOnly ) )
{
for(i=0;i<5;i++)
{
iIndex=i;
int ch;
ch=filetime.getch();
iTime[iIndex]=ch-65;
}
}
filetime.close();
Play->setEnabled(FALSE);
Record->setEnabled(FALSE);
Stop->setEnabled(FALSE);
Clean->setEnabled(FALSE);
};

void MyWidget::lRecord() //录音
{
 int fd;      /* 声音设备的文件描述符 */
        int arg;     /* 用于ioctl调用的参数 */
       int status;   /* 系统调用的返回值 */

 /* 打开声音设备 */
 fd = open("/dev/dsp", O_RDWR);
 if (fd < 0)
{
    perror("open of /dev/dsp failed");
    exit(1);
 }
 /* 设置采样时的量化位数 */
 arg = SIZE;
 status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
 if (status == -1)
    perror("SOUND_PCM_WRITE_BITS ioctl failed");
 if (arg != SIZE)
    perror("unable to set sample size");
 /* 设置采样时的声道数目 */
 arg = CHANNELS; 
 status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
 if (status == -1)
    perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
 if (arg != CHANNELS)
    perror("unable to set number of channels");
 /* 设置采样时的采样频率 */
 arg = RATE;
 status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
 if (status == -1)
    perror("SOUND_PCM_WRITE_WRITE ioctl failed");
   pcmfd = open("./sounds/3.wav", O_CREAT | O_RDWR | O_TRUNC, 7666);
    status = read(fd, buf, sizeof(buf)); /* 录音 */
for(i=0;i<5;i++)
{

if(ListBox->isSelected(i)&&ListBox->text(i).isEmpty())
{
iIndex=i;
Record->setEnabled( FALSE );
Play->setEnabled( FALSE );
iState=1; //录音状态
time1.start();
iBar=0;
timer->singleShot( 0,this,SLOT(lBar()) );
}
}
     Play->setEnabled( TRUE );

};

void MyWidget::lBar() //进度条
{
if(iState==1&&iBar<=300) //录音时进度条的变化
{
label4->setGeometry( 95, 88, iBar, 16);
label4->setBackgroundColor(QColor( 255, 0, 0));
iBar=iBar+1;
timer->singleShot( 100,this,SLOT(lBar()) );
}
if(iState==2&&iBar<=iPlBar) //播放时进度条的变化
{
label6->setGeometry( 95, 88, iBar, 16);
label6->setBackgroundColor(QColor( 0, 0, 255));
iBar=iBar+1;
timer->singleShot( 100,this,SLOT(lBar()) );
}
if(iState==1&&iBar>300) //30秒到进度停止
{
timer->singleShot(0,this,SLOT(lStop()));
}
if(iState==2&&iBar>iPlBar)
{
timer->singleShot(0,this,SLOT(lStop()));
}
};

void MyWidget::lPlay() //播放
{
for(i=0;i<5;i++)
{
if(ListBox->isSelected(i)&&!ListBox->text(i).isEmpty())
{
iIndex=i;
iBar=0;
iState=2; //播放状态
QSound::play("./sounds/3.wav");
//QSound::play(cStr);
iPlBar=iTime[iIndex]*1000/100;
label4->setGeometry( 95, 88, iPlBar, 16);
label4->setBackgroundColor(QColor( 255, 0, 0));
Play->setEnabled(FALSE);
Record->setEnabled(FALSE);
sprintf(cStr,"%d.wav",iIndex);
timer->singleShot( 0,this,SLOT(lBar()) );
}
}
Record->setEnabled(TRUE);
};

void MyWidget::lStop() //停止
{
if(iState==1) //录音状态                  
{
rStop();
iState=0;
label4->setGeometry( 95, 88, 0, 16);
}
else if(iState==2) //播放状态             
{
pStop();
iState=0;
label4->setGeometry( 95, 88, 0, 16);
label6->setGeometry( 95, 88, 0, 16);
}
};

void MyWidget::rStop() //录音停止
{
iTime[iIndex]=time1.elapsed()/1000;
if(iBar>300)
iTime[iIndex]=30;
sprintf(cStr,"%d.wav  time %d",iIndex,iTime[iIndex]);
ListBox->changeItem(cStr,iIndex);
Record->setEnabled(TRUE);
};

void MyWidget::pStop()
{
Play->setEnabled(TRUE);
};

void MyWidget::lClean() //清除
{
if(iState==1) //录音状态时
{
sprintf(cStr,"%d.wav",iIndex); 
Remove->remove(cStr);
iState=0;
label4->setGeometry( 95, 88, 0, 16);
Record->setEnabled(TRUE);
}
if(iState==2) //播放状态时
{
for(i=0;i<5;i++)
{
if(ListBox->isSelected(i)&&!ListBox->text(i).isEmpty())
{
iIndex=i;
pStop();
sprintf(cStr,"%d.wav",iIndex); 
Remove->remove(cStr);
ListBox->changeItem("",iIndex);
iState=0;
label4->setGeometry( 95, 88, 0, 16);
label6->setGeometry( 95, 88, 0, 16);
Play->setEnabled(TRUE);
}
}
}
if(iState==0) //平常状态时
{
for(i=0;i<5;i++)
{
if(ListBox->isSelected(i)&&!ListBox->text(i).isEmpty())
{
iIndex=i;
sprintf(cStr,"%d.wav",iIndex); 
Remove->remove(cStr);
ListBox->changeItem("",iIndex);
}
}
}
};

void MyWidget::lQuit() //退出
{

QFile file( "file.txt" );
QStringList::Iterator it = lines.begin();
if ( file.open( IO_WriteOnly ) ) 
{
for(i=0;i<5;i++)
{
iIndex=i;
QTextStream stream( &file );
if(ListBox->text(iIndex).isEmpty())
stream << *it << "\n";
else
stream << *it << ListBox->text(iIndex)+"\n";
it++;
}
}
file.close();

QFile filet( "time.txt" );
if ( filet.open( IO_WriteOnly ) ) 
{
for(i=0;i<5;i++)
{
iIndex=i;
int ch=iTime[iIndex]+65;
if(ListBox->text(iIndex).isEmpty())
filet.putch(65);
else
filet.putch(ch);
}
}
filet.close();
timer->singleShot( 0,qApp,SLOT(quit()));
};

--------------------编程问答-------------------- void MyWidget::lVolUp() //调节音量(大)
{
iVol=iVol+10;
if(iVol>=80)
   iVol=80;
label5->setGeometry( 470, 88, iVol, 16);
};

void MyWidget::lVolDown() //调节音量(小)
{
iVol=iVol-10;
label5->setGeometry( 470, 88, iVol, 16);
if(iVol<=0)
   iVol=0;
};

void MyWidget::lAffirm()
{
Affirm->setGeometry( 500, 20, 40, 20 );
Affirm->setFont( QFont( "Times", 10, QFont::Bold ) );
QPixmap pix_Affirm("./pic/ready.bmp");
Affirm->setPixmap(pix_Affirm);
label9->setGeometry( 600, 440, 40, 40);
QPixmap pix_label9("./pic/deng1.bmp");
label9->setPixmap(pix_label9);
if (connect( Affirm, SIGNAL(clicked()), this, SLOT(rAffirm()) ));
};

void MyWidget::rAffirm()
{
Play->setEnabled(TRUE);
Record->setEnabled(TRUE);
Stop->setEnabled(TRUE);
Clean->setEnabled(TRUE);
Affirm->setGeometry( 500, 20, 40, 20 );
Affirm->setFont( QFont( "Times", 10, QFont::Bold ) );
QPixmap pix_Affirm("./pic/start.bmp");
Affirm->setPixmap(pix_Affirm);
Affirm->setEnabled(FALSE);
label9->setGeometry( 600, 440, 40, 40);
QPixmap pix_label9("./pic/deng.bmp");
label9->setPixmap(pix_label9);
};
补充:移动开发 ,  Qt
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,