WebRTC学习之三:录音和播放

VoiceEngine中与录音和播放相关的头文件有五个,如下表所示:

头文件

包含的类

说明

voe_base.h

VoiceEngineObserver

VoiceEngine

VoEBase


1.默认使用G.711通过RTP进行全双工的VoIP会话
2.初始化和终止
3.通过文件和回调函数跟踪信息
4.多通道支持(比如混合,发送到多个目的端)
5.如果想支持G.711外的编码,需要VoECodec

voe_errors.h


一些VoiceEngine相关错误的定义

voe_file.h

VoEFile

1.音频播放

2.音频录制

3.音频转换

voe_hardware.h

VoEHardware

1.管理音频设备

2.获取设备信息

3.采样率设置

voe_volume_control.h

VoEVolumeControl

1.扬声器音量控制 

2.麦克风音量控制

3.非线性语音电平控制

4.静音

5.音量放大

   
   
   
   
   
   
 
一.环境

WebRTC版本:2016.6.22,详见 WebRTC学习之二:编译

Qt版本:Qt5.7.0 VS2015

需要安装Microsoft DirectX SDK (June 2010),否则链接的时候会报:

LNK2019:无法解析的外部符号_MoInitMediaType@8,该符号在......

LNK2019:无法解析的外部符号_MoFreeMediaType@8,该符号在......

LNK2019:无法解析的外部符号_IID_IMediaBuffer

LNK2019:无法解析的外部符号_IID_IMediaObject

LNK2019:无法解析的外部符号_CLSID_CWMAudioAEC

如下图所示:


二.实现

mainwindow.h

[cpp]  view plain  copy
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.   
  4. #include <QMainWindow>  
  5. #include "myobserver.h"  
  6. #include "webrtc/voice_engine/include/voe_base.h"  
  7. #include "webrtc/voice_engine/include/voe_errors.h"  
  8. #include "webrtc/voice_engine/include/voe_file.h"  
  9. #include "webrtc/voice_engine/include/voe_hardware.h"  
  10. #include "webrtc/voice_engine/include/voe_volume_control.h"  
  11. using namespace webrtc;  
  12.   
  13. namespace Ui {  
  14. class MainWindow;  
  15. }  
  16.   
  17. class MainWindow : public QMainWindow  
  18. {  
  19.     Q_OBJECT  
  20.   
  21. public:  
  22.     explicit MainWindow(QWidget *parent = 0);  
  23.     ~MainWindow();  
  24.   
  25. private:  
  26.     void creatVoiceEngine();  
  27.     int initialVoiceEngine();  
  28.     int setDevice();  
  29.     void setChannel();  
  30.     int getMicrophoneVolumeValue();  
  31.     int getSpeakerVolumeValue();  
  32.     int unInitialVoiceEngine();  
  33.   
  34. private:  
  35.     Ui::MainWindow *ui;  
  36.   
  37.     MyObserver myObserver;  
  38.     int error;  
  39.     int audioChannel;  
  40.   
  41.     VoiceEngine* ptrVoEngine;  
  42.     VoEBase* ptrVoEBase;  
  43.     VoEVolumeControl* ptrVoEVolumeControl;  
  44.     VoEFile* ptrVoEFile;  
  45.     VoEHardware* ptrVoEHardware;  
  46.   
  47. private slots:  
  48.     void on_pushButtonRecording_clicked();  
  49.     void on_pushButtonPlayout_clicked();  
  50.     void slotSetMicrophoneVolumeValue(int value);  
  51.     void slotSetSpeakerVolumeValue(int value);  
  52. };  
  53.   
  54. #endif // MAINWINDOW_H  

mainwindow.cpp

[cpp]  view plain  copy
  1. #include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3. #include <QDebug>  
  4. MainWindow::MainWindow(QWidget *parent) :  
  5.     QMainWindow(parent),  
  6.     ui(new Ui::MainWindow),  
  7.     error(0),  
  8.     audioChannel(0),  
  9.     ptrVoEngine(NULL),  
  10.     ptrVoEBase(NULL),  
  11.     ptrVoEVolumeControl(NULL),  
  12.     ptrVoEFile(NULL),  
  13.     ptrVoEHardware(NULL)  
  14. {  
  15.     ui->setupUi(this);  
  16.     creatVoiceEngine();  
  17.     initialVoiceEngine();  
  18.     setDevice();  
  19.     setChannel();  
  20.   
  21.     connect(ui->horizontalSliderMicrophoneVolume,SIGNAL(valueChanged(int)),this,SLOT(slotSetMicrophoneVolumeValue(int)));  
  22.     connect(ui->horizontalSliderSpeakerVolume,SIGNAL(valueChanged(int)),this,SLOT(slotSetSpeakerVolumeValue(int)));  
  23.   
  24.     int vol=getMicrophoneVolumeValue();  
  25.     ui->horizontalSliderMicrophoneVolume->setValue(vol);  
  26.     ui->lineEditMicrophoneVolumeValue->setText(QString::number(vol));  
  27.   
  28.     vol=getSpeakerVolumeValue();  
  29.     ui->horizontalSliderSpeakerVolume->setValue(vol);  
  30.     ui->lineEditSpeakerVolumeValue->setText(QString::number(vol));  
  31. }  
  32.   
  33. MainWindow::~MainWindow()  
  34. {  
  35.     delete ui;  
  36.     unInitialVoiceEngine();  
  37. }  
  38.   
  39. void MainWindow::creatVoiceEngine()  
  40. {  
  41.     ptrVoEngine = VoiceEngine::Create();  
  42.     ptrVoEBase = VoEBase::GetInterface(ptrVoEngine);  
  43.     ptrVoEVolumeControl = VoEVolumeControl::GetInterface(ptrVoEngine);  
  44.     ptrVoEFile = VoEFile::GetInterface(ptrVoEngine);  
  45.     ptrVoEHardware = VoEHardware::GetInterface(ptrVoEngine);  
  46. }  
  47.   
  48. int MainWindow::initialVoiceEngine()  
  49. {  
  50.     error = ptrVoEBase->Init();  
  51.     if (error != 0)  
  52.     {  
  53.         qDebug()<<"ERROR in VoEBase::Init";  
  54.         return error;  
  55.     }  
  56.     error = ptrVoEBase->RegisterVoiceEngineObserver(myObserver);  
  57.     if (error != 0)  
  58.     {  
  59.         qDebug()<<"ERROR in VoEBase:;RegisterVoiceEngineObserver";  
  60.         return error;  
  61.     }  
  62.     char temp[1024];  
  63.     error = ptrVoEBase->GetVersion(temp);  
  64.     if (error != 0)  
  65.     {  
  66.         qDebug()<<"ERROR in VoEBase::GetVersion";  
  67.         return error;  
  68.     }  
  69.   
  70.     ui->lineEditVersion->setText(QString(temp));  
  71.     return 100;  
  72. }  
  73.   
  74. int MainWindow::unInitialVoiceEngine()  
  75. {  
  76.     //Stop Playout  
  77.     error = ptrVoEBase->StopPlayout(audioChannel);  
  78.     if (error != 0)  
  79.     {  
  80.         qDebug()<<"ERROR in VoEBase::StopPlayout";  
  81.         return error;  
  82.     }  
  83.     error = ptrVoEFile->StopPlayingFileLocally(audioChannel);  
  84.     if (error != 0)  
  85.     {  
  86.         qDebug()<<"ERROR in VoEFile::StopPlayingFileLocally";  
  87.         return error;  
  88.     }  
  89.     //Stop Record  
  90.     error = ptrVoEFile->StopRecordingMicrophone();  
  91.     if (error != 0)  
  92.     {  
  93.         qDebug()<<"ERROR in VoEFile::StopRecordingMicrophone";  
  94.         return error;  
  95.     }  
  96.   
  97.     //Delete Channel  
  98.     error = ptrVoEBase->DeleteChannel(audioChannel);  
  99.     if (error != 0)  
  100.     {  
  101.         qDebug()<<"ERROR in VoEBase::DeleteChannel";  
  102.         return error;  
  103.     }  
  104.     //DeRegister observer  
  105.     ptrVoEBase->DeRegisterVoiceEngineObserver();  
  106.     error = ptrVoEBase->Terminate();  
  107.     if (error != 0)  
  108.     {  
  109.         qDebug()<<"ERROR in VoEBase::Terminate";  
  110.         return error;  
  111.     }  
  112.   
  113.     if(ptrVoEBase)  
  114.     {  
  115.         ptrVoEBase->Release();  
  116.     }  
  117.   
  118.     if(ptrVoEVolumeControl)  
  119.     {  
  120.         ptrVoEVolumeControl->Release();  
  121.     }  
  122.   
  123.     if(ptrVoEFile)  
  124.     {  
  125.         ptrVoEFile->Release();  
  126.     }  
  127.   
  128.     if(ptrVoEHardware)  
  129.     {  
  130.         ptrVoEHardware->Release();  
  131.     }  
  132.   
  133.     bool flag = VoiceEngine::Delete(ptrVoEngine);  
  134.     if (!flag)  
  135.     {  
  136.         qDebug()<<"ERROR in VoiceEngine::Delete";  
  137.         return -1;  
  138.     }  
  139.     return 100;  
  140. }  
  141.   
  142. int MainWindow::setDevice()  
  143. {  
  144.     int rNum(-1), pNum(-1);  
  145.     error = ptrVoEHardware->GetNumOfRecordingDevices(rNum);  
  146.     if (error != 0)  
  147.     {  
  148.         qDebug()<<"ERROR in VoEHardware::GetNumOfRecordingDevices";  
  149.         return error;  
  150.     }  
  151.     error = ptrVoEHardware->GetNumOfPlayoutDevices(pNum);  
  152.     if (error != 0)  
  153.     {  
  154.         qDebug()<<"ERROR in VoEHardware::GetNumOfPlayoutDevices";  
  155.         return error;  
  156.     }  
  157.   
  158.     char name[128] = { 0 };  
  159.     char guid[128] = { 0 };  
  160.   
  161.     for (int j = 0; j < rNum; ++j)  
  162.     {  
  163.         error = ptrVoEHardware->GetRecordingDeviceName(j, name, guid);  
  164.         if (error != 0)  
  165.         {  
  166.             qDebug()<<"ERROR in VoEHardware::GetRecordingDeviceName";  
  167.             return error;  
  168.         }  
  169.         ui->comboBoxRecordingDevice->addItem(QString(name));  
  170.     }  
  171.   
  172.     for (int j = 0; j < pNum; ++j)  
  173.     {  
  174.         error = ptrVoEHardware->GetPlayoutDeviceName(j, name, guid);  
  175.         if (error != 0)  
  176.         {  
  177.             qDebug()<<"ERROR in VoEHardware::GetPlayoutDeviceName";  
  178.             return error;  
  179.         }  
  180.         ui->comboBoxPlayoutDevice->addItem(QString(name));  
  181.     }  
  182.   
  183.     error = ptrVoEHardware->SetRecordingDevice(ui->comboBoxRecordingDevice->currentIndex());  
  184.     if (error != 0)  
  185.     {  
  186.         qDebug()<<"ERROR in VoEHardware::SetRecordingDevice";  
  187.         return error;  
  188.     }  
  189.   
  190.     error = ptrVoEHardware->SetPlayoutDevice(ui->comboBoxPlayoutDevice->currentIndex());  
  191.     if (error != 0)  
  192.     {  
  193.         qDebug()<<"ERROR in VoEHardware::SetPlayoutDevice";  
  194.         return error;  
  195.     }  
  196.   
  197.     return 100;  
  198. }  
  199.   
  200. void MainWindow::setChannel()  
  201. {  
  202.     audioChannel = ptrVoEBase->CreateChannel();  
  203.     if (audioChannel < 0)  
  204.     {  
  205.         qDebug()<<"ERROR in VoEBase::CreateChannel";  
  206.     }  
  207.     error = ptrVoEBase->StartPlayout(audioChannel);  
  208.     if(error != 0)  
  209.     {  
  210.         qDebug()<<"ERROR in VoEBase::StartPlayout";  
  211.     }  
  212. }  
  213.   
  214. int MainWindow::getMicrophoneVolumeValue()  
  215. {  
  216.     unsigned int vol = 999;  
  217.     error = ptrVoEVolumeControl->GetMicVolume(vol);  
  218.     if (error != 0)  
  219.     {  
  220.         qDebug()<<"ERROR in VoEVolume::GetMicVolume";  
  221.         return 0;  
  222.     }  
  223.     if ((vol > 255) || (vol < 0))  
  224.     {  
  225.         qDebug()<<"ERROR in GetMicVolume";  
  226.         return 0;  
  227.     }  
  228.     return vol;  
  229. }  
  230.   
  231. int MainWindow::getSpeakerVolumeValue()  
  232. {  
  233.     unsigned int vol = 999;  
  234.     error = ptrVoEVolumeControl->GetSpeakerVolume(vol);  
  235.     if (error != 0)  
  236.     {  
  237.         qDebug()<<"ERROR in VoEVolume::GetSpeakerVolume";  
  238.         return 0;  
  239.     }  
  240.     if ((vol > 255) || (vol < 0))  
  241.     {  
  242.         qDebug()<<"ERROR in GetSpeakerVolume";  
  243.         return 0;  
  244.     }  
  245.     return vol;  
  246. }  
  247.   
  248. void MainWindow::on_pushButtonRecording_clicked()  
  249. {  
  250.     static bool flag=true;  
  251.     if(flag)  
  252.     {  
  253.         //录制麦克风的音频,默认采样率是8000HZ  
  254.         error = ptrVoEFile->StartRecordingMicrophone("RecordingMicrophone.pcm");  
  255.         if (error != 0)  
  256.         {  
  257.             qDebug()<<"ERROR in VoEFile::StartRecordingMicrophone";  
  258.         }  
  259.         else  
  260.         {  
  261.             ui->pushButtonRecording->setText(QStringLiteral("停止录音"));  
  262.         }  
  263.     }  
  264.     else  
  265.     {  
  266.         error = ptrVoEFile->StopRecordingMicrophone();  
  267.         if (error != 0)  
  268.         {  
  269.             qDebug()<<"ERROR in VoEFile::StopRecordingMicrophone";  
  270.         }  
  271.         else  
  272.         {  
  273.             ui->pushButtonRecording->setText(QStringLiteral("开始录音"));  
  274.         }  
  275.     }  
  276.   
  277.     flag=!flag;  
  278. }  
  279.   
  280. void MainWindow::on_pushButtonPlayout_clicked()  
  281. {  
  282.     static bool flag=true;  
  283.     if(flag)  
  284.     {  
  285.         error = ptrVoEFile->StartPlayingFileLocally(audioChannel,"RecordingMicrophone.pcm");  
  286.         if (error != 0)  
  287.         {  
  288.             qDebug()<<"ERROR in VoEFile::StartPlayingFileLocally";  
  289.         }   
  290.         else  
  291.         {  
  292.   
  293.              ui->pushButtonPlayout->setText(QStringLiteral("停止播放"));  
  294.         }  
  295.     }  
  296.     else  
  297.     {  
  298.         error = ptrVoEFile->StopPlayingFileLocally(audioChannel);  
  299.         if (error != 0)  
  300.         {  
  301.             qDebug()<<"ERROR in VoEFile::StopPlayingFileLocally";  
  302.         }  
  303.         else  
  304.         {  
  305.             ui->pushButtonPlayout->setText(QStringLiteral("开始播放"));  
  306.         }  
  307.     }  
  308.   
  309.     flag=!flag;  
  310. }  
  311.   
  312. void MainWindow::slotSetMicrophoneVolumeValue(int value)  
  313. {  
  314.     error = ptrVoEVolumeControl->SetMicVolume(value);  
  315.     if (error != 0)  
  316.     {  
  317.         qDebug()<<"ERROR in VoEVolume::SetMicVolume";  
  318.     }  
  319.     else  
  320.     {  
  321.         ui->lineEditMicrophoneVolumeValue->setText(QString::number(value));  
  322.     }  
  323. }  
  324. void MainWindow::slotSetSpeakerVolumeValue(int value)  
  325. {  
  326.     error = ptrVoEVolumeControl->SetSpeakerVolume(value);  
  327.     if (error != 0)  
  328.     {  
  329.         qDebug()<<"ERROR in VoEVolume::SetSpeakerVolume";  
  330.     }  
  331.     else  
  332.     {  
  333.         ui->lineEditSpeakerVolumeValue->setText(QString::number(value));  
  334.     }  
  335. }  

三.效果


通过“录音音量”滑块可以实时调节录音的音量大小(0—255)。

通过“播放音量”滑块可以调节播放的音量大小(0—255),类似音乐播放器的音量调节,这里只是调节本软件的音量,而不是调节音频设备的音量。


源码链接:见http://blog.csdn.net/caoshangpa/article/details/53446916的评论



版权声明:本文为灿哥哥http://blog.csdn.net/caoshangpa原创文章,转载自 : https://blog.csdn.net/caoshangpa/article/details/53446916
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值