How To Play Sound Effect

This how to shows you how to play a sound effect in SwinGame. A sound effect is different to playing music. A sound effect will play over multiple channels which means you can play multiple sound effects simultaneously. Sound effects can be useful for giving the user feedback like a sound if a button is pressed, or when an event has completed. 

Level

Beginner

Read these first

About Audio

Details

To play a sound effect you must load it into the game loop. Sounds are stored in the Resources/sounds sub folder

The simplest method to play a sound effect is to Load Sound Effect('...') then call Play Sound Effect('...'). You notice we are using the file name as the name of the effect to play.

To keep track and reuse sound effects without having to reload them each time you may choose to load the sound effect into a variable, like this:

 myEffect equals Load Sound Effect('...') then call:

Play Sound Effect(myEffect).

This way myEffect can be used each time you need to play the effect without needing to load it every time.

Source Code Widget

  • program HowToPlaySoundEffect;
    uses SwinGame;
    
    procedure Main();
    
    begin
        OpenAudio();
        OpenGraphicsWindow('How To Play Sound Effect' ,320 ,240 );
        LoadDefaultColors();
        ClearScreen(ColorWhite );
        LoadSoundEffect('chipmunk.ogg' );
        PlaySoundEffect('chipmunk.ogg' );
        DrawText('How To Play Sound Effect!!!' ,ColorRed ,10 ,120 );
        RefreshScreen();
        Delay(5000 );
        CloseAudio();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
    
        open_audio();
        open_graphics_window("How To Play Sound Effect" ,320 ,240 );
        load_default_colors();
        clear_screen(color_white );
        load_sound_effect("chipmunk.ogg" );
        play_sound_effect("chipmunk.ogg" );
        draw_text("How To Play Sound Effect!!!" ,color_red ,10 ,120 );
        refresh_screen();
        delay(5000 );
        close_audio();
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

Under Windows a window need to be open in order for it to play audio

Possibilities

  • Create special sound effect in game such as explosion, bullet flying.
  • Interactivity between player and in game object using sound effect.