How to: Use Engine Callbacks

You can notify the XAudio2 client code of engine events by registering an instance of a class implementing the IXAudio2EngineCallback interface with the XAudio2 engine. This allows the XAudio2 client code to keep track of when audio processing is occurring, and when to restart the engine in the event of a critical error.

To use an engine callback

The following steps register an object to handle engine events.

  1. Create a class that inherits from the IXAudio2EngineCallback interface.

    All methods of IXAudio2EngineCallback are purely virtual and must be defined. The method of interest in this example is IXAudio2EngineCallback::OnCriticalError, which sets a flag to signal the main game loop that a critical error has occurred. The remaining methods, IXAudio2EngineCallback::OnProcessingPassStart and IXAudio2EngineCallback::OnProcessingPassEnd, are stubs in this example.

    class EngineCallback : public IXAudio2EngineCallback
        void OnProcessingPassEnd () {}
        void OnProcessingPassStart() {}
        void OnCriticalError (HRESULT Error) {}
  2. Use XAudio2Create to create an instance of the XAudio2 engine.

    if ( FAILED(hr = XAudio2Create( &pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) )
        return hr;
  3. Use IXAudio2::RegisterForCallbacks to register the engine callback.

    pXAudio2->RegisterForCallbacks( &engineCallback );
  4. If you don't need the engine callback any more, call IXAudio2::UnregisterForCallbacks.

    pXAudio2->UnregisterForCallbacks( &engineCallback );

Related topics

XAudio2 Callbacks
XAudio2 Programming Guide
How to: Build a Basic Audio Processing Graph
How to: Stream a Sound from Disk