Ask me!‎ > ‎

XNA Music Hitching and Sounds Not Stopping XAct

posted Mar 24, 2012, 5:46 PM by Derick Janssen   [ updated Mar 24, 2012, 5:52 PM ]
Q:
I moved away from MediaPlayer for background music due to the hitching it creates when starting/stopping songs even on high-end systems under Win7/Vista. This has been documented in many threads and I guess it is a Windows MediaPlayer issue that is unfixable:

http://forums.create.msdn.com/forums/t/80427.aspx
http://forums.create.msdn.com/forums/p/45977/275034.aspx
http://forums.create.msdn.com/forums/p/69556/424388.aspx
http://forums.create.msdn.com/forums/p/44169/419462.aspx

Anyway, I have converted my .mp3 files to .wav and compressed them in XACT according to this thread:

http://blogs.msdn.com/b/mitchw/archive/2007/04/27/audio-compression-using-xact.aspx

So the songs are bigger than mp3 but still reasonable. Now I'm having some issues stopping and starting music cues. I'm using the following code which is based on the audio samples and many threads I've read here:


static Cue musicCue;

     //---------------------------------------------------------------- 
    // PlayMusicTrack() - stop current track if any 
    //                  - play new track 
    //---------------------------------------------------------------- 
    public static void PlayMusicTrack(string cueName) 
    { 
        if (musicCue != null && musicCue.IsPlaying) 
        { 
            musicCue.Stop(AudioStopOptions.Immediate); 
            musicCue.Dispose(); 
            musicCue = null; 
        } 
        musicCue = SoundManager.soundBank.GetCue(cueName); 
        musicCue.Play();  
    } 

But unfortunately, this doesn't work at all. The idea is that only a single music track will ever be playing, so I will just fecth a new Cue every time a new song is played. The following issues occur though:

- Cue.Stop() does not stop anything
- after the first Cue.Play(), subsequent Play() calls inexplicably start multiple song cues which also cannot be stopped.
 
There's a ton of threads on here about Cue.Stop() wonkiness so there must be some trick to getting it to work correctly.



A:
You shouldn't dispose it unless you plan on not using it anymore or reloading it later (which will hitch).

Hope this helps,
Derick

        public static void stopMusic() 
        { 
            if (MediaPlayer.State == MediaState.Playing) 
            { 
                MediaPlayer.Stop(); 
            } 
            music = null; 
        } 
 
        public static void stopSound(string fileNameMinusExtension) 
        { 
            soundBank.GetCue(fileNameMinusExtension).Stop(AudioStopOptions.AsAuthored); 
            soundBank.GetCue(fileNameMinusExtension).Stop(AudioStopOptions.Immediate); 
        } 
        public static void stopAllSounds() 
        { 
            for (int i = 0; i < NUM_SOUNDS; ++i) 
            { 
                stopSound(fileNames[i]); 
            } 
            stopMusic(); 
        } 


Comments