I want to write a program that can play many sounds by pressing some keys.But I found that after I pressed keys 64 times,the program couldn’t play any sounds! I have loaded all the sounds and I also declared channel and I use this channel to play each of sounds.the following is code:
int main()
{
FMOD_RESULT result;
FMOD::System *system = NULL;
result = FMOD::System_Create(&system);
if (result != FMOD_OK)
{
std::cout << “FMOD error!” << result << FMOD_ErrorString(result) << std::endl;
}
result = system->init(512, FMOD_INIT_NORMAL, 0);
if (result != FMOD_OK)
{
std::cout << “FMOD error!” << result << FMOD_ErrorString(result) << std::endl;
}
FMOD::Sound *sound[24];
string soundsign[2][13] = { { "C1", C1,SC1,D1,SD1,E1,F1,SF1,G1,SG1,A1,SA1,B1},
{ "C",C,SC,D,SD,E,F,SF,G,SG,A,SA,B } };
string a;
std::cin >> a;
int a_value = 0;
for (int i = 0; i < 2; i++)
{
if (soundsign[i][0] == a)
{
for (int j = 1; j < 13; j++)
{
system->createSound(soundsign[i][j].c_str(), FMOD_DEFAULT, NULL, &sound[j - 1]);
}
a_value = 1;
}
}
FMOD::Channel *channel[24];
int ch = 0, cnt = 0;
while (ch != 27)
{
ch = _getch();
switch (ch)
{
case 'z':if (a_value == 0) break; else { result = system->playSound(sound[0], NULL, 0, &channel[0]); break; }
case 'x':if (a_value == 0) break; else { result=system->playSound(sound[1], NULL, 0, &channel[0]); break; }
case 'c':if (a_value == 0) break; else { result = system->playSound(sound[2], NULL, 0, &channel[0]);break; }
case 'v':if (a_value == 0) break; else { result = system->playSound(sound[3], NULL, 0, &channel[0]);break; }
case 'b':if (a_value == 0) break; else { result = system->playSound(sound[4], NULL, 0, &channel[0]); break; }
case 'n':if (a_value == 0) break; else { result = system->playSound(sound[5], NULL, 0, &channel[0]);break; }
case 'm':if (a_value == 0) break; else { result = system->playSound(sound[6], NULL, 0, &channel[0]);break; }
case ',':if (a_value == 0) break; else { result = system->playSound(sound[7], NULL, 0, &channel[0]);break; }
case '.':if (a_value == 0) break; else { result = system->playSound(sound[8], NULL, 0, &channel[0]);break; }
case '/':if (a_value == 0) break; else { result = system->playSound(sound[9], NULL, 0, &channel[0]);break; }
case 32:if (a_value == 0) break; else { result = system->playSound(sound[10], NULL, 0, &channel[0]);break; }
case 13:if (a_value == 0) break; else { result = system->playSound(sound[11], NULL, 0, &channel[0]);break; }
default:
break;
} //在弹奏一段时间后会出现不出声的情况,result也没有返回不正常。
cnt++;
cout << cnt << endl;
if (result != FMOD_OK)
{
std::cout << "error!" << std::endl;
break;
}
}
std::system("pause");
system->release();
return 0;
}