XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

XNA 3.0 sound loop while key is pressed

Last post 09-10-2008 1:14 AM by Ashu Tatake. 1 replies.
  • 09-04-2008 11:09 PM

    XNA 3.0 sound loop while key is pressed

    Hi,

    I am using XNA 3.0 CTP and am wondering if it is possible to play a sound in a loop while a key is pressed down without using XACT. I know how to do it using XACT but looks like sound use is simplified in 3.0 CTP.

    If I use SoundEffect.Play(); it sounds like a mess. I need the sound to play at its regular speed in a loop while a key is pressed.

    Thanks,

    Kamal

    http://www.sciencesquare.com
  • 09-10-2008 1:14 AM In reply to

    Re: XNA 3.0 sound loop while key is pressed

    Hi Kamal, SoundEffect.Play has an overload that allows you to loop a sound.

    Here's some basic sample code that should do what you want (of course, sfx and sfxi are SoundEffect and SoundEffectInstance types that you'll need to define in your game).

               if (Keyboard.GetState().IsKeyDown(Keys.Space)) 
                { 
                    if (sfxi == null
                    { 
                        sfxi = sfx.Play(1.0f, 0, 0, true); 
                    } 
                    else 
                    { 
                        sfxi.Play(); 
                    } 
                } 
                else if (Keyboard.GetState().IsKeyUp(Keys.Space)) 
                { 
                    if (sfxi != null
                    { 
                        sfxi.Stop(true); 
                    } 
                } 

    - Ashu Tatake
    XNA Framework Developer
Page 1 of 1 (2 items) Previous Next