【发布时间】:2020-05-16 19:16:16
【问题描述】:
当我尝试在 AudioSpec 结构中分配回调变量时,当我尝试将类型“void 函数”分配给类型 SDL_AudioCallback 时,编译器不喜欢它。
void mainEngineCW4::myAudioCallback(void* userdata, Uint8* stream, int Glen) {
AudioData* audio = (AudioData*)userdata;
if (audio->length == 0)
return;
Uint32 length = (Uint32)len;
length = (length > audio->length ? audio->length : length); // if length is more than the audio length, then set length to be the audio.length, if not, set it to be the length passed to the function
SDL_memcpy(stream, audio->position, length);
audio->position += length;
audio->length -= length; }
void mainEngineCW4::playAudio() // this function is for loading an audio device, and playing the audio through that device {
AudioData audio;
audio.position = wavStart;
audio.length = wavLength;
wavSpec.callback = mainEngineCW4::myAudioCallback;
wavSpec.userdata = &audio;
audioDevice = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE);
if (audioDevice == 0)
{
std::cerr << SDL_GetError() << std::endl;
return;
}
SDL_PauseAudioDevice(audioDevice, 0); // mildly confused by why they decided to call the function for starting to play audio for "PauseAudioDevice" but yeah. this plays audio. }
我将控制音频的职责分为三个函数,loadAudio、startAudio 和 endAudio。我已经在 .h 文件中分配了音频所需的变量,因此类中的所有函数都可以访问它。
【问题讨论】: