我正在编写一个 C++ WinRT 组件 DLL 用于我的基于 .NET 的 WinRT 应用程序。DLL 定义了一个 SoundSample 引用类,该类通过调用IXAudio2::CreateSourceVoice创建 XAudio 语音。CreateSourceVoice 采用“IXAudio2VoiceCallback *pCallback”参数来启用各种音频事件的回调。现在我正在尝试根据这篇文章实现该回调。XAudio 应该只是回调我的 SoundCallback 类的方法,定义为:
#pragma once
#include "xaudio2.h"
#include "pch.h"
class SoundCallback
: public IXAudio2VoiceCallback
{
private:
//SoundSample^ sample; //does not compile
public:
SoundCallback(void);
~SoundCallback(void);
//Called when the voice has just finished playing a contiguous audio stream.
void OnStreamEnd();
void OnVoiceProcessingPassEnd();
void OnVoiceProcessingPassStart(UINT32 SamplesRequired);
void OnBufferEnd(void * pBufferContext);
void OnBufferStart(void * pBufferContext);
void OnLoopEnd(void * pBufferContext);
void OnVoiceError(void * pBufferContext, HRESULT Error);
};
一切都很好,直到我试图弄清楚如何从我的本机回调类的实例回调到父 SoundSample 对象。我在想我可以将 SoundSample 类的实例传递给 SoundCallback 对象,但似乎它不允许我在本机类中声明 ref 类字段:
SoundCallback.h(9): error C2143: syntax error : missing ';' before '^'
SoundCallback.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SoundCallback.h(9): error C3699: '^' : cannot use this indirection on type 'int'
我回顾了在本机 C++ 中实现回调,到目前为止我找不到合理的解决方案。最好/最简单的方法是什么?