打开关闭输入输出音频设备

<1> : 打开音频输出设备WINAPI :

MMRESULT waveOutOpen(
  LPHWAVEOUT
phwo,   //返回设备句柄的指针,传入地址;        
  UINT uDeviceID,    //设备ID号,数值大小-1<uDeviceID<waveOutGetNumDevs()返回值;        
  LPWAVEFORMATEX pwfx, //WAVEFORMATEX格式指针,申请波形格式;      
  DWORD dwCallback,    //回调函数,如果不使用回调函数机制,设为NULL;      
  DWORD dwCallbackInstance//回调函数事例数据,不用于窗口; 
  DWORD fdwOpen  //打开选项;           
);

返回值为0,即打开设备成功!

上面参数中WAVEFORMATEX设置一般如下:

WAVEFORMATEX m_wfx;

m_wfx.nSamplesPerSec = 44100; /* sample rate */
m_wfx.wBitsPerSample = 16; /* sample size */
m_wfx.nChannels = 2; /* channels*/

m_wfx.cbSize = 0; /* size of _extra_ info */
m_wfx.wFormatTag = WAVE_FORMAT_PCM;
m_wfx.nBlockAlign = (m_wfx.wBitsPerSample >> 3) * m_wfx.nChannels;
m_wfx.nAvgBytesPerSec = m_wfx.nBlockAlign * m_wfx.nSamplesPerSec;

c>.关闭设备

MMRESULT waveOutClose(
  HWAVEOUT
hwo 
);
参数传入打开设备时返回的设备句柄参数即可.

<2> : 再新声明一个设备句柄变量:

HWAVEOUT hWaveOut;

<3> : 打开设备:

if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &m_wfx, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR) {
 fprintf(stderr, "unable to open WAVE_MAPPER device\n");
}

<4> : 输出设备Demo :

#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
HWAVEOUT hWaveOut; /* device handle */
WAVEFORMATEX wfx; /* look this up in your documentation */
MMRESULT result;/* for waveOut return values */
/*
* first we need to set up the WAVEFORMATEX structure.
* the structure describes the format of the audio.
*/
wfx.nSamplesPerSec = 44100; /* sample rate */
wfx.wBitsPerSample = 16; /* sample size */
wfx.nChannels = 2; /* channels*/
/*
* WAVEFORMATEX also has other fields which need filling.
* as long as the three fields above are filled this should
* work for any PCM (pulse code modulation) format.
*/
wfx.cbSize = 0; /* size of _extra_ info */
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
/*
* try to open the default wave device. WAVE_MAPPER is
* a constant defined in mmsystem.h, it always points to the
* default wave device on the system (some people have 2 or
* more sound cards).
*/
if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR) {
 fprintf(stderr, "unable to open WAVE_MAPPER device\n");
 ExitProcess(1);
}
/*
* device is now open so print the success message
* and then close the device again.
*/
printf("The Wave Mapper device was opened successfully!\n");
waveOutClose(hWaveOut);
return 0;
}

<5> : 输入设备

MMRESULT waveInOpen(
  LPHWAVEIN
phwi,           
  UINT uDeviceID,           
  LPWAVEFORMATEX pwfx,      
  DWORD dwCallback,         
  DWORD dwCallbackInstance
  DWORD fdwOpen             
);
参数基本含义基本差不多.

关闭设备:

MMRESULT waveInClose(
  HWAVEIN
hwi 
);

 

 

转载于:https://www.cnblogs.com/MMLoveMeMM/articles/3104294.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值