我们已经定义了我们的基本代码如下。当我们相应地按下键时,代码片段会播放音乐卡盘。现在的问题是 file1 较短,而 file2 稍长。我们需要混合两者并播放它们,以延长 file1 的持续时间以匹配 file2。如何通过 sdl 做?
Mix_Chunk *file1 = NULL;
Mix_Chunk *file2 = NULL;
file1 = Mix_LoadWAV( "file1.wav" );
file2 = Mix_LoadWAV( "file2.wav" );
while( quit == false )
{
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//If 1 was pressed
if( event.key.keysym.sym == SDLK_1 )
{
//Play the scratch effect
if( Mix_PlayChannel( -1, file1, 0 ) == -1 )
{
return 1;
}
}
//If 2 was pressed
else if( event.key.keysym.sym == SDLK_2 )
{
//Play the high hit effect
if( Mix_PlayChannel( -1, file2, 0 ) == -1 )
{
return 1;
}
}
}
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}