|
我想通过回调函数来判断FMOD_CHANNEL_CALLBACKTYPE_END的状态,确定一个声音是否结束,如果一个声音结束,马上播放另一个声音。主程序中使用WaitForSingleObject (hSoundOverEvent, INFINITE)来等待回调函数的状态。现在的问题是:主程序停在WaitForSingleObject处,不能实现循环播放。查看帮助文档,说channel::setCallback必须依赖System::update,也就是说只有当声音播放完毕后你还要调用System::update,才能得到FMOD_CHANNEL_CALLBACKTYPE_END的状态。可是如果这样就失去了WaitForSingleObject的意义,因为其不能自动触发。
请问:怎么才能用FMOD实现两个声音的循环播放呢?(注:不用播放列表技术。)代码如下
include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "../../api/inc/fmod.hpp"
#include "../../api/inc/fmod_errors.h"
void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}
HANDLE hSoundOverEvent = NULL;
/* 一个声音片段播放完毕时回调*/
FMOD_RESULT F_CALLBACK callbackChannel(FMOD_CHANNEL * channel, FMOD_CHANNEL_CALLBACKTYPE type, void * commanddata1, void * commanddata2)
{
FMOD::Channel *cppchannel = (FMOD::Channel *)channel;
if(type == FMOD_CHANNEL_CALLBACKTYPE_END)
{
SetEvent(hSoundOverEvent);
};
return FMOD_OK;
}
int main(int argc, char *argv[])
{
/************************************
Create sound system
***********************************/
FMOD::System *system;
FMOD::Sound *sound1, *sound2;
FMOD::Channel *channel;
FMOD_RESULT result;
int key;
FMOD_MODE modeTyre = FMOD_LOOP_OFF | FMOD_SOFTWARE;
result = FMOD::System_Create(&system);
ERRCHECK(result);
result = system->init(32, FMOD_INIT_NORMAL, 0);
ERRCHECK(result);
result = system->createSound("../wavesource/v40_20ms.wav", modeTyre, 0, &sound1);
ERRCHECK(result);
result = system->createSound("../wavesource/v80_20ms.wav", modeTyre, 0, &sound2);
ERRCHECK(result);
result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel);
ERRCHECK(result);
int i= 1;
do
{
if(_kbhit())
{
key = getch();
}
if((i & 1) == 0)
result = system->playSound(FMOD_CHANNEL_FREE, sound2, false, &channel);
else
result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel);
i +=1;
channel->setCallback(callbackChannel);
system->update();
WaitForSingleObject (hSoundOverEvent, INFINITE);
} while (key != 27);
/* Shut down */
result = sound1->release();
ERRCHECK(result);
result = sound2->release();
ERRCHECK(result);
result = system->close();
ERRCHECK(result);
result = system->release();
return 0;
}
abcabc |
|