黑猫先知 2015-04-06 05:59 采纳率: 0%
浏览 1944

调用directsound实现音频播放,编译通过,运行出错?

以下是我自己尝试写的调用direct sound实现播放音频文件代码,编译是通过了,但执行后出现错误~不知道是什么原因~请高手指点~万分感谢

void CAudioPlayerDlg::OnButtonPlay()
{
// TODO: Add your control notification handler code here

LPDIRECTSOUND lpds = NULL;

InitDSound(m_hWnd, lpds);
CAudioPlayerDlg::lpdsb[0] = LoadStatic(lpds,"Audio.wav");
PlayStatic(CAudioPlayerDlg::lpdsb[0]);

}

BOOL CAudioPlayerDlg::InitDSound(HWND hwnd, LPDIRECTSOUND lpds)
{
HRESULT hr;

// Create DirectSound.
if ( FAILED( hr = DirectSoundCreate(NULL, &lpds, NULL ) ) )
    return FALSE;


// Set cooperative level.
if ( FAILED( hr = lpds->SetCooperativeLevel(hwnd, DSSCL_PRIORITY ) ) )
    return FALSE;

return TRUE;

}

LPDIRECTSOUNDBUFFER CAudioPlayerDlg::LoadStatic (LPDIRECTSOUND lpds, LPSTR lpzFileName)
{
WAVEFORMATEX *pwfx;
HMMIO hmmio;
MMCKINFO mmckinfo;
MMCKINFO mmckinfoParent;
LPDIRECTSOUNDBUFFER lpdsbStatic = NULL;
LPVOID lpvAudio1;
DWORD dwBytes1;
DSBUFFERDESC dsbdesc;
UINT cbBytesRead;

if (WaveOpenFile(lpzFileName, &hmmio, &pwfx, &mmckinfoParent ) != 0 )
    return FALSE;

if (WaveStartDataRead( &hmmio, &mmckinfo, &mmckinfoParent ) != 0 )
    return FALSE;

if ( lpdsbStatic == NULL )
{
    memset( &dsbdesc, 0, sizeof( DSBUFFERDESC ) ); 
    dsbdesc.dwSize = sizeof( DSBUFFERDESC ); 
    dsbdesc.dwFlags = DSBCAPS_STATIC; 
    dsbdesc.dwBufferBytes = mmckinfo.cksize; 
    dsbdesc.lpwfxFormat = pwfx; 
    if ( FAILED( lpds->CreateSoundBuffer(&dsbdesc, &lpdsbStatic, NULL ) ) )
    {
        WaveCloseReadFile(&hmmio, &pwfx);
        return FALSE; 
    }
}

lpdsbStatic->Lock( 0, 0, &lpvAudio1, &dwBytes1, NULL, NULL, DSBLOCK_ENTIREBUFFER );
WaveReadFile( hmmio, dwBytes1, ( BYTE * )lpvAudio1,&mmckinfo, &cbBytesRead );
lpdsbStatic->Unlock( lpvAudio1, dwBytes1, NULL, 0 );
WaveCloseReadFile( &hmmio, &pwfx );

return lpdsbStatic;

}

void CAudioPlayerDlg::PlayStatic(LPDIRECTSOUNDBUFFER lpdsbStatic)
{
if ( lpdsbStatic == NULL ) return;
lpdsbStatic->SetCurrentPosition(0);
lpdsbStatic->Play(0, 0, 0);
}

图片说明
图片说明
图片说明

已解决~

  • 写回答

2条回答 默认 最新

  • oyljerry 2015-04-06 06:08
    关注

    先说说运行出错的具体代码行数。看是不是指针访问有问题

    评论

报告相同问题?

悬赏问题

  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并