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

Random Background Music

Last post 07/11/2009 19:25 by Mouse88. 4 replies.
  • 06/11/2009 20:31

    Random Background Music

    I have a few background tracks which I want to play at random. I know how the create the XACT project and set the cues to infinite loop. Can anyone advise a good approach to: A) Randomly select one background cue to play, B) Stop one of these cues when someone wins
    Speed - Now In Peer Review
  • 06/11/2009 20:51 In reply to

    Re: Random Background Music

    If you're using XACT, then the process is fairly simple:

    1) Add your music files to the Wave Bank
    2) Add each of them as a Sound in the Sound Bank
    3) Create a new Cue, call it "background_music" or something
    4) Drag each of the pieces of music from the Sound Bank into the right panel next to the Cue. They should each be added and have a "probability" number attached to them.

    You can now edit the probabilities individually if you want to, but more importantly a random piece of music will be played whenever you call Cue.Play("background_music"). Be sure to set each of them under the "Background Music" group if you want to properly support Custom Soundtracks.

    2) To stop the music when you want, you need to save the Cue object when playing it. Then you can call Cue.Stop later on to stop the music:
    Cue cue; 
     
    void StartLevel() 
        Cue cue2 = soundBank.GetCue("background_music"); 
     
        //stop any music that might be playing 
        if (cue != null
        { 
            if(cue.IsPlaying) 
                cue.Stop(AudioStopOptions.Immediate); 
        } 
        cue = cue2; 
        cue.Play(); 
     
    void EndLevel() 
        if (cue != null
        { 
            if (cue.IsPlaying) 
                cue.Stop(AudioStopOptions.AsAuthored); 
        } 

    "No programmer can pick up a TV remote without thinking what it would take to add a stun gun. [...] Their motto is 'if it ain't broke, it doesn't have enough features yet'" - Scott Adams, The Dilbert Principle

    The signature that was too big for the 512 char limit
  • 07/11/2009 16:00 In reply to

    Re: Random Background Music

    I am having some slight problems with this. I have my background music running randomly but when I play a sound effect its getting a different backround track and playing it over the other one.

    Heres what im doing:

    Select Single Player Mode:

    if (BackCue != null)  
    {  
        if (BackCue.IsPlaying)  
        {  
            BackCue.Stop(AudioStopOptions.AsAuthored);  
        }  
    }  
     
    BackCue = soundBank.GetCue("BackgroundMusic");  
    BackCue.Play(); 

    This only runs once as I have run through with a break point.

    Play A Sound Effect:

    soundBank.PlayCue("FlipCard"); 

    I have put a breakpoint on this line of code above and for some reason it plays another background track on top of the other one when im not evening telling it to to anything with the BackCue.

    Can anyone give me a clue as to whats going wrong?
    Speed - Now In Peer Review
  • 07/11/2009 17:47 In reply to

    Re: Random Background Music

    It sounds like your FlipCard cue does more than just play a track with a "flipping card" wave on it. Or you have somehow turned on instance limiting. Or there's some bug in your code, such that you're also re-playing the background music cue.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 07/11/2009 19:25 In reply to

    Re: Random Background Music

    Problem solved Id accidentally dropped the three background wav's into the flip card cue.
    Speed - Now In Peer Review
Page 1 of 1 (5 items) Previous Next