游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4304|回复: 4

关于SetVolume的问题

[复制链接]

3

主题

19

帖子

19

积分

新手上路

Rank: 1

积分
19
发表于 2008-9-12 16:58:00 | 显示全部楼层 |阅读模式
if (KEYDOWN(buffer, DIK_DOWN))
{

        curVol=curVol-1000;
        hr=DSBuffer->SetVolume(curVol);
                               
        if FAILED(hr)
        {
        if(hr==DSERR_INVALIDPARAM)
              return FALSE;
        }
}
这是Beginning DirecX9这本书中的一个例子 ,原例只是循环的播放一个声音

为了实现用方向键递减音量。我加了关于Input Keyboard的代码

各位前辈请看上面的代码 如果我把断点打在这句 "hr=DSBuffer->SetVolume(curVol);"

然后再执行。就没有问题。(curVol每次递减1000)



可是断点打在if(hr==DSERR_INVALIDPARAM) 或 return FALSE这两句;

curVol的值就成了"11000"

于是得到一个DSERR_INVALIDPARAM  然后程序退出。

直接运行也是这个结果(程序直接退出)。

如果注释掉return FALSE 声音就消失了。



麻烦各位大侠前辈救救小弟。。。被这问题困扰了一下午。。。

以下是全部源代码
#include <windows.h>
#define DIRECTINPUT_VERSION 0x0800
#include <dxerr9.h>
#include <dsound.h>
#include <dinput.h>

#include "dsutil.h"

#include <string>
using namespace std;

// Window functions and variables
HINSTANCE hInst;                                // holds the instance for this app
HWND wndHandle;                                        // global window handle

LPDIRECTINPUT8        g_lpDI;        // the direct input object
LPDIRECTINPUTDEVICE8  g_lpDIDevice; // the direct input device
char     buffer[256];
#define KEYDOWN(name, key) (name[key] & 0x80)

bool    initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

// DirectSound functions and variables
LPDIRECTSOUND8        g_pDS;        // The DirectSound Device



bool initDirectSound(HWND hwnd);
void shutdownDirectSound(void);
LPDIRECTSOUNDBUFFER LoadWaveToSoundBuffer(std::string wavFilename);
void playSound(LPDIRECTSOUNDBUFFER whichBuffer);
void playSoundLoop(LPDIRECTSOUNDBUFFER whichBuffer);
void stopSound(LPDIRECTSOUNDBUFFER whichBuffer);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
        HRESULT hr;
        static long curVol=0;

        // call our function to init and create our window
        if (!initWindow(hInstance))
        {
                MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
                return 0;
        }

        // initialize DirectSound
        if (!initDirectSound(wndHandle))
        {
                MessageBox(NULL, "Unable to init DirectSound", "ERROR", MB_OK);
                return 0;
        }

        // load a wave file into a directsound buffer
        LPDIRECTSOUNDBUFFER DSBuffer = LoadWaveToSoundBuffer("sound1.wav");

        if (!DSBuffer)
        {
                MessageBox(NULL, "Unable to load sound1.wav", "ERROR", MB_OK);
                return 0;
        }

        hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION,
                IID_IDirectInput8, (void**)&g_lpDI, NULL);

        if FAILED(hr) return FALSE;

        // Retrieve a pointer to an IDirectInputDevice8 interface
        hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL);

        hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard);

        if FAILED(hr) {
                return FALSE;
        }

        // Set the cooperative level
        hr = g_lpDIDevice->SetCooperativeLevel(wndHandle,
                DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
        if FAILED(hr)
        {
                return FALSE;
        }

        // Get access to the input device.
        hr = g_lpDIDevice->Acquire();
        if FAILED(hr)
        {
                return FALSE;
        }

       
        hr = DSBuffer->SetVolume(curVol);
        if FAILED(hr)
        {
                if(hr==DSERR_INVALIDPARAM)
                        return FALSE;
        }
        // Main message loop:
        // Enter the message loop
        MSG msg;
        ZeroMemory( &msg, sizeof(msg) );

        while( msg.message!=WM_QUIT )
        {
                // check for messages
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                        TranslateMessage( &msg );
                        DispatchMessage( &msg );
                }
                else
                {
                        // play this sound
                        hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);
                        // check the return state to see if the device is still accessible
                        if (FAILED ( hr ))
                        {
                                // try and reacquire the input device
                                hr = g_lpDIDevice->Acquire();
                                // do a continuous loop until the device is reacquired
                                while( hr == DIERR_INPUTLOST )
                                        hr = g_lpDIDevice->Acquire();

                                // just continue and do nothing this frame
                                continue;
                        }               


                        if (KEYDOWN(buffer, DIK_DOWN))
                        {

                                curVol=curVol-1000;
                                hr=DSBuffer->SetVolume(curVol);
                               
                                if FAILED(hr)
                                {
                                        if(hr==DSERR_INVALIDPARAM)
                                                return FALSE;
                                }
                        }


                        playSoundLoop(DSBuffer);

                }
        }

        // Release the DirectSound buffer created above
        if (DSBuffer)
        {
                DSBuffer->Release();
                DSBuffer = NULL;
        }

        // shutdown DirectSound
        shutdownDirectSound();

        return (int) msg.wParam;
}

/*******************************************************************
* shutdownDirectSound
* Releases the DirecSound device
*******************************************************************/
void shutdownDirectSound(void)
{
        if (g_pDS)
        {
                g_pDS->Release();
                g_pDS = NULL;
        }
}

/*******************************************************************
* initDirectSound
* Initializes DirectSound
*******************************************************************/
bool initDirectSound(HWND hwnd)
{
        HRESULT hr;

        hr = DirectSoundCreate8( NULL, &g_pDS, NULL );
        if FAILED (hr)
                return false;

        // Set DirectSound cooperative level
        hr = g_pDS->SetCooperativeLevel( hwnd, DSSCL_PRIORITY );
        if FAILED ( hr )
                return false;

        return true;
}

/*******************************************************************
* LoadWaveToSoundBuffer
* Loads a wave file into a DirectSound Buffer
*******************************************************************/
LPDIRECTSOUNDBUFFER LoadWaveToSoundBuffer(std::string wavFilename)
{
        LPDIRECTSOUNDBUFFER apDSBuffer = NULL;
        CWaveFile *wavFile;
        HRESULT hr;

        wavFile = new CWaveFile();
        wavFile->Open((char*)wavFilename.c_str(), NULL, WAVEFILE_READ );
        if( wavFile->GetSize() == 0 )
        {
                MessageBox(wndHandle, "invalid file", "ERROR", MB_OK);
                return false;
        }

        DSBUFFERDESC dsbd;
        ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
        dsbd.dwSize          = sizeof(DSBUFFERDESC);
        dsbd.dwFlags         = DSBCAPS_CTRLVOLUME;
        dsbd.dwBufferBytes   = wavFile->GetSize();
        dsbd.guid3DAlgorithm = GUID_NULL;
        dsbd.lpwfxFormat     = wavFile->m_pwfx;

        hr = g_pDS->CreateSoundBuffer( &dsbd, &apDSBuffer, NULL );
        if FAILED (hr)
        {
                MessageBox(NULL, "unable to create sound buffer", "ERROR", MB_OK);
                return NULL;
        }

        VOID*   pDSLockedBuffer      = NULL; // Pointer to locked buffer memory
        DWORD   dwDSLockedBufferSize = 0;    // Size of the locked DirectSound buffer
        DWORD   dwWavDataRead        = 0;    // Amount of data read from the wav file

        hr = apDSBuffer->Lock( 0, wavFile->GetSize(),
                &pDSLockedBuffer, &dwDSLockedBufferSize,
                NULL, NULL, 0L );
        if FAILED (hr)
                return NULL;

        // Reset the wave file to the beginning
        wavFile->ResetFile();

        // Read the wave file
        hr = wavFile->Read( (BYTE*) pDSLockedBuffer,
                dwDSLockedBufferSize,
                &dwWavDataRead );
        // Check to make sure that this was successful
        if FAILED (hr)
                return NULL;

        // Check to make sure the wav file is not empty
        if( dwWavDataRead == 0 )
        {
                // Wav is blank, so just fill with silence
                FillMemory( (BYTE*) pDSLockedBuffer,
                        dwDSLockedBufferSize,
                        (BYTE)(wavFile->m_pwfx->wBitsPerSample == 8 ? 128 : 0 ) );
        }
        else if( dwWavDataRead < dwDSLockedBufferSize )
        {
                // Don't repeat the wav file, just fill in silence
                FillMemory( (BYTE*) pDSLockedBuffer + dwWavDataRead,
                        dwDSLockedBufferSize - dwWavDataRead,
                        (BYTE)(wavFile->m_pwfx->wBitsPerSample == 8 ? 128 : 0 ) );
        }

        // Unlock the buffer, we don't need it anymore.
        apDSBuffer->Unlock( pDSLockedBuffer, dwDSLockedBufferSize, NULL, 0 );

        // Clean up
        delete wavFile;

        return apDSBuffer;
}

/*******************************************************************
* playSound
* plays a sound currently in a buffer only once
*******************************************************************/
void playSound(LPDIRECTSOUNDBUFFER whichBuffer)
{
        whichBuffer-&gtlay( 0, 0, 0);
}

/*******************************************************************
* playSoundLoop
* plays a sound in a buffer repeatedly
*******************************************************************/
void playSoundLoop(LPDIRECTSOUNDBUFFER whichBuffer)
{
        whichBuffer->Play( 0, 0, DSBPLAY_LOOPING );
}

/*******************************************************************
* stopSound
* stops the sound in this buffer from playing
*******************************************************************/
void stopSound(LPDIRECTSOUNDBUFFER whichBuffer)
{
        whichBuffer->Stop();
}

/*******************************************************************
* initWindow
* inits and creates the window
*******************************************************************/
bool initWindow(HINSTANCE hInstance)
{
        WNDCLASSEX wcex;

        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style                        = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc        = (WNDPROC)WndProc;
        wcex.cbClsExtra                = 0;
        wcex.cbWndExtra                = 0;
        wcex.hInstance                = hInstance;
        wcex.hIcon                        = 0;
        wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName        = NULL;
        wcex.lpszClassName        = "DirectXExample";
        wcex.hIconSm                = 0;
        RegisterClassEx(&wcex);

        wndHandle = CreateWindow("DirectXExample",
                "DirectXExample",
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                640,
                480,
                NULL,
                NULL,
                hInstance,
                NULL);
        if (!wndHandle)
                return false;

        ShowWindow(wndHandle, SW_SHOW);
        UpdateWindow(wndHandle);

        return true;
}

/*******************************************************************
* WndProc
* The window procedure for this window
*******************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        switch (message)
        {
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
        }
        return DefWindowProc(hWnd, message, wParam, lParam);
}

3

主题

19

帖子

19

积分

新手上路

Rank: 1

积分
19
 楼主| 发表于 2008-9-13 09:02:00 | 显示全部楼层

Re: 关于SetVolume的问题

大哥们。。。这是我在本论坛提的第一个问题啊。。。

无人鸟我么 - -。。

0

主题

1

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2008-9-13 16:52:00 | 显示全部楼层

Re:关于SetVolume的问题

如果是调节系统音量(任务栏里面那个小喇叭),我也遇到过一些问题,就是不能平滑调节,音量不是最大就是最小。去CSDN等地方也问过,没什么结果……

3

主题

19

帖子

19

积分

新手上路

Rank: 1

积分
19
 楼主| 发表于 2008-9-14 08:37:00 | 显示全部楼层

Re:关于SetVolume的问题

呃。。。我觉得是运行环境和调试环境的问题。。。

谢谢楼上的了~ 其实也明白这只是小问题。继续学下去说不定哪天就明白了。但是还是有点不甘心~

4

主题

12

帖子

12

积分

新手上路

Rank: 1

积分
12
发表于 2009-3-21 02:57:00 | 显示全部楼层

Re: 关于SetVolume的问题

我也有此问题,在google上找个列子,正在啃,你需要联系我
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-20 01:51

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表