我现在是通过网络接收到音频数据,请问如何用DirectSound来播放,哪位大哥有这方面的例子,请发一份给我,谢谢
DirectSound中有一个例子StreamData,但是是播放Wave文件的,我是将Socket收到的音频数据用DirectSound来播放,不知如何改.
[email protected]

解决方案 »

  1.   

    //--------------------------------------------------------------------------------------
    //
    // Function to cleanup allocated objects
    //
    //--------------------------------------------------------------------------------------
    void vCleanup( void )
    {
    if( g_pSound ) {
    if( g_pPerformance ) 
    g_pSound->Unload( g_pPerformance );
    }
    SAFE_RELEASE( g_pSound ); SAFE_RELEASE( g_pLoader );
    SAFE_RELEASE( g_pPerformance );
    }//--------------------------------------------------------------------------------------
    //
    // Function to initialize the sound system and load the various
    // sounds needed for the game.
    //
    //--------------------------------------------------------------------------------------
    bool bInitializeSoundSystem( HWND hWnd ) 
    {
    HRESULT hResult;
    IDirectMusicAudioPath8 *dmAudioPath; // Initialize COM
    CoInitialize( NULL );

    // Create the loader
    if( FAILED( hResult = CoCreateInstance( CLSID_DirectMusicLoader, NULL, 
    CLSCTX_INPROC, IID_IDirectMusicLoader8,
    (void**) &g_pLoader ) ) ) {
    return( 0 );
    }

    // Create the performance
    if( FAILED( hResult = CoCreateInstance( CLSID_DirectMusicPerformance, NULL,
    CLSCTX_INPROC, IID_IDirectMusicPerformance8,
    (void**) &g_pPerformance ) ) ) {
    return( 0 );
    }

    // Initialize the audio
    if( FAILED( hResult = g_pPerformance->InitAudio( 
    NULL,
    NULL,
    hWnd,
    DMUS_APATH_DYNAMIC_STEREO,
    4,
    DMUS_AUDIOF_ALL,
    NULL
    ))) {
    return( 0 );
    }

    // Get the default path
    if( FAILED( g_pPerformance->GetDefaultAudioPath( &dmAudioPath ) ) ) 
    return( 0 );

    // Set the default volume
    if( FAILED( dmAudioPath->SetVolume(0,0) ) ) 
    return( 0 ); // Load the sound from a file
    if ( FAILED(g_pLoader->LoadObjectFromFile (
    CLSID_DirectMusicSegment,
    IID_IDirectMusicSegment8,
    L"testsound.wav",
    ( LPVOID* ) &g_pSound
    ) ) )
    {
    return( 0 );
    }

    // Download the data
    if ( FAILED ( g_pSound->Download( g_pPerformance ) ) ) {
    return( 0 );
    } // Return success
    return( 1 );
    }//--------------------------------------------------------------------------------------
    //
    // Function to play the global sound file
    //
    //--------------------------------------------------------------------------------------
    void vPlaySound( void )
    {
    // Play the sound segment
    g_pPerformance->PlaySegmentEx(
    g_pSound,
    NULL,
    NULL,
    DMUS_SEGF_DEFAULT | DMUS_SEGF_SECONDARY,
    0,
    NULL,
    NULL,
    NULL
    );
    }
      

  2.   

    上面是dmusic播放,下面是dsound
      

  3.   

    //-----------------------------------------------------------------------------
    // Name: SoundSystem::hrInitSoundSystem()
    // Desc: Initializes Direct Audio
    //-----------------------------------------------------------------------------
    HRESULT SoundSystem::hrInitSoundSystem(void)
    {
    HRESULT hResult;
    IDirectMusicAudioPath8 *path; // Initialize COM
    CoInitialize(NULL);

    // Create the loader
    if( FAILED( hResult = CoCreateInstance(CLSID_DirectMusicLoader, NULL, 
    CLSCTX_INPROC, IID_IDirectMusicLoader8,
    (void**)&pLoader) )) {
    return(SOUNDERROR_MUSICLOADER);
    }

    // Create the performance
    if( FAILED( hResult = CoCreateInstance(CLSID_DirectMusicPerformance, NULL,
    CLSCTX_INPROC, IID_IDirectMusicPerformance8,
    (void**)&pPerformance ))) {
    return(SOUNDERROR_MUSICPERFORMANCE);
    }

    // Initialize the audio
    if( FAILED( hResult = pPerformance->InitAudio( 
    NULL,
    NULL,
    hWnd,
    DMUS_APATH_DYNAMIC_STEREO,
    4,
    DMUS_AUDIOF_ALL,
    NULL
    ))) {
    return(SOUNDERROR_INITAUDIO);
    }

    // Get the default path
    if( FAILED( pPerformance->GetDefaultAudioPath( &path ) ) ) 
    return(SOUNDERROR_PATH);

    // Set the default volume
    if( FAILED( path->SetVolume(0,0) ) ) 
    return(SOUNDERROR_VOLUME); return(S_OK);
    }//-----------------------------------------------------------------------------
    // Name: SoundSystem::hrLoadSound()
    // Desc: Loads a sound and stores it in the GameSound passed
    //-----------------------------------------------------------------------------
    HRESULT SoundSystem::hrLoadSound(char *szname,GameSound *gs)
    {
    WCHAR szWideFileName[ 512 ]; // Make sure audio is initialized
    if( !pLoader ) 
    return(SOUNDERROR_MUSICLOADER); if( !pPerformance ) 
    return(SOUNDERROR_MUSICPERFORMANCE); // Clean up sound if it already exists
    if( gs->pSound ) {
    gs->pSound->Unload( pPerformance );
    gs->pSound->Release();
    gs->pSound = NULL;
    }

    // Copy in the filename
    DXUtil_ConvertGenericStringToWideCch( szWideFileName, szname, 512 ); // Load the sound from a file
    if ( FAILED(pLoader->LoadObjectFromFile (
    CLSID_DirectMusicSegment,
    IID_IDirectMusicSegment8,
    szWideFileName,
    ( LPVOID* ) &gs->pSound
    ) ) )
    {
    return( SOUNDERROR_LOAD );
    }

    gs->pPerformance = pPerformance; // Download the data
    if ( FAILED ( gs->pSound->Download( pPerformance ) ) ) {
    return(SOUNDERROR_DOWNLOAD);
    }

    return(S_OK);
    }//-----------------------------------------------------------------------------
    // Name: SoundSystem::hrPlaySound()
    // Desc: Plays a GameSound segment object
    //-----------------------------------------------------------------------------
    HRESULT SoundSystem::hrPlaySound(GameSound *gs)
    {
    // Make sure there is a performance object present
    if( !pPerformance ) 
    return( SOUNDERROR_MUSICPERFORMANCE );

    if( !gs->pSound )
    return( SOUNDERROR_NOSEGMENT );

    // Play the sound segment
    if( FAILED ( pPerformance->PlaySegmentEx(
    gs->pSound,
    NULL,
    NULL,
    DMUS_SEGF_DEFAULT | DMUS_SEGF_SECONDARY,
    0,
    NULL,
    NULL,
    NULL
    ))) 
    return( SOUNDERROR_PLAYFAIL );

    return(S_OK); 
    }