#include "wdef.h" #include "mmsplayer_wave.h" #include <Windows.h>
/*
版权: Weiny Zhou
联系方式: QQ514540005
声明:未经允许不得擅自发布本代码的部分或全部。
*/ typedef struct wAudio_wave { int sample; int channel; int bits; int buffersize; LPWAVEHDR lpWaveHdr; unsigned char* buffer[2]; w_wave_callback callback; void* pParam; WAVEFORMATEX sFormat; HWAVEOUT hWaveOut; }wAudio_wave; static inline void CALLBACK w_wave_auido_Proc( HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 ) { WAVEHDR *wHdr; wAudio_wave * pAudio =NULL; switch (uMsg) { case WOM_OPEN: break; case WOM_CLOSE: break; case WOM_DONE: { LPWAVEHDR pWaveHeader = (LPWAVEHDR)dwParam1; pAudio=(wAudio_wave*)dwInstance; waveOutUnprepareHeader( hwo, pWaveHeader, sizeof(WAVEHDR)); pWaveHeader->dwBufferLength = pAudio->callback(pAudio->pParam,pWaveHeader->lpData,pAudio->buffersize); waveOutPrepareHeader( hwo, pWaveHeader, sizeof(WAVEHDR)); waveOutWrite( hwo, pWaveHeader, sizeof(WAVEHDR) ); } break; default: break; } } wAudio w_wave_auido_open(int sample,int channel,int bits,int bytesper,int align,int bufsize,void* pParam,w_wave_callback func) { wAudio_wave* pAudio=NULL; WAVEOUTCAPS caps; INTOFUNC(); if(waveOutGetDevCaps(WAVE_MAPPER,&caps,sizeof(WAVEOUTCAPS))){ WERROR_A("waveOutGetDevCaps error.");goto error_lab; } pAudio=WOS_MALLOC_(wAudio_wave,1); if(!pAudio){ PRINTF_ERROR_VALUE(ERROR_NEWMEM);goto error_lab; } memset(pAudio,0,sizeof(wAudio_wave)); pAudio->sFormat.wFormatTag=1; pAudio->sFormat.nChannels=pAudio->channel=channel; pAudio->sFormat.nSamplesPerSec=pAudio->sample=sample; pAudio->sFormat.wBitsPerSample=pAudio->bits=bits; pAudio->sFormat.nAvgBytesPerSec=bytesper; pAudio->sFormat.nBlockAlign=align; pAudio->buffersize=bufsize; pAudio->callback=func; pAudio->pParam=pParam; pAudio->sFormat.cbSize=sizeof(WAVEFORMATEX); pAudio->buffer[0]=WOS_MALLOC_(unsigned char,pAudio->buffersize); if(pAudio->channel==1) pAudio->lpWaveHdr=WOS_MALLOC_(WAVEHDR,1); else { pAudio->lpWaveHdr=WOS_MALLOC_(WAVEHDR,2); pAudio->buffer[1]=WOS_MALLOC_(unsigned char,pAudio->buffersize); } if(!pAudio||!pAudio->buffer[0]||(pAudio->channel==2&&!pAudio->buffer[1])){ PRINTF_ERROR_VALUE(ERROR_NEWMEM);w_wave_audio_close(pAudio);goto error_lab; } if(waveOutOpen(&pAudio->hWaveOut,WAVE_MAPPER,&pAudio->sFormat,w_wave_auido_Proc,pAudio,CALLBACK_FUNCTION)) { SAFE_FREE(pAudio); WERROR_A("waveOutOpen error.");goto error_lab; } memset(pAudio->lpWaveHdr,0,sizeof(WAVEHDR)); pAudio->lpWaveHdr[0].lpData=pAudio->buffer[0]; pAudio->lpWaveHdr[0].dwFlags=0; pAudio->lpWaveHdr[0].dwLoops==0; pAudio->lpWaveHdr[0].dwBufferLength=func(pParam,pAudio->buffer[0],bufsize); if(pAudio->channel==2){ memset(pAudio->lpWaveHdr+1,0,sizeof(WAVEHDR)); pAudio->lpWaveHdr[1].dwFlags=0; pAudio->lpWaveHdr[1].dwLoops=0; pAudio->lpWaveHdr[1].lpData=pAudio->buffer[1]; pAudio->lpWaveHdr[1].dwBufferLength=func(pParam,pAudio->buffer[1],bufsize); waveOutPrepareHeader(pAudio->hWaveOut, pAudio->lpWaveHdr, sizeof(WAVEHDR)); waveOutPrepareHeader(pAudio->hWaveOut, pAudio->lpWaveHdr+1, sizeof(WAVEHDR)); waveOutWrite(pAudio->hWaveOut, pAudio->lpWaveHdr, sizeof(WAVEHDR)); waveOutWrite(pAudio->hWaveOut, pAudio->lpWaveHdr+1, sizeof(WAVEHDR)); } else if(pAudio->channel==1) { waveOutPrepareHeader(pAudio->hWaveOut, pAudio->lpWaveHdr, sizeof(WAVEHDR)); waveOutWrite(pAudio->hWaveOut, pAudio->lpWaveHdr, sizeof(WAVEHDR)); } error_lab: EXITFUNC(); return pAudio; } int w_wave_audio_close(wAudio audio) { int nResult=0; wAudio_wave* pAudio=NULL; INTOFUNC(); if(audio){ nResult=ERROR_INITPARAM;PRINTF_ERROR_VALUE(nResult);goto error_lab;} pAudio=(wAudio_wave*)audio; if(pAudio->channel==0)goto error_lab; if(pAudio->hWaveOut) { waveOutReset(pAudio->hWaveOut); waveOutPrepareHeader(pAudio->hWaveOut, &pAudio->lpWaveHdr, sizeof(WAVEHDR)); if(pAudio->channel==2) waveOutPrepareHeader(pAudio->hWaveOut, &pAudio->lpWaveHdr+1, sizeof(WAVEHDR)); waveOutClose(pAudio->hWaveOut); } SAFE_FREE(pAudio->lpWaveHdr); SAFE_FREE(pAudio->buffer[0]); SAFE_FREE(pAudio->buffer[1]); error_lab: SAFE_FREE(pAudio); EXITFUNC(); return nResult; }
这是mmsplayer for windows 的声音接口,喜欢的拿去用。
至于ios的前面已经上传过了。
android的昨天也上传了。
版权所有,禁止转载或发布本篇文章的部分或全部。