Jacob Parker
Jacob Parker

Reputation: 13

Windows Phone 7 Multiple Sounds Lag

I'm making a drum machine. From what I've read, it looks like the XNA SoundEffect class runs based on a timer, which causes a noticeable lag, and stops the rhythm being smooth.

I tried to use MediaElement, 'til I found out you cannot play multiple sounds at the same time.

Are there any workarounds for this? The sounds are handled by a timer, and need to play instantly.

Upvotes: 1

Views: 657

Answers (2)

johnhforrest
johnhforrest

Reputation: 991

I've been using some sound in my app and I used the code from the example given on the msdn code samples website: http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx

It looks like they are updating the timer every 50ms. Also note that the SoundEffect variables (coyoteSound and birdSound) are private data members where they only load once. The event handler on the button clicks simply call SoundEffect.play().

public MainPage()
{
    InitializeComponent();

    LoadSound("Resources/NightAmbientCreatureOneShot_01.wav", out coyoteSound);
    LoadSound("Resources/AfternoonAmbientBirdOneShot_09.wav", out birdSound);
    LoadSoundInstance("Resources/NightAmbienceSimple_01.wav", out ambienceSound, out ambienceInstance);

    // Set the volume a little lower than full so it becomes the background.
    ambienceInstance.Volume = 0.8f;

    // Turn on looping so it runs continually in the background.
    ambienceInstance.IsLooped = true;

    // Timer to simulate the XNA game loop (SoundEffect classes are from the XNA Framework)
    DispatcherTimer XnaDispatchTimer = new DispatcherTimer();
    XnaDispatchTimer.Interval = TimeSpan.FromMilliseconds(50);

    // Call FrameworkDispatcher.Update to update the XNA Framework internals.
    XnaDispatchTimer.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };

    // Start the DispatchTimer running.
    XnaDispatchTimer.Start();
}

Upvotes: 0

Stuart
Stuart

Reputation: 66882

I've done some in-game use of the XNA SoundEffect class and not seen any lag when responding to user events - e.g. button presses - especially when the sound effect is pre-loaded from resources.

The XNA class is designed to be used for sound effects - so it should be ideal for a single drum machine hit.

If you then see problems with timing on IsLooping, then I guess you'll have to implement your own timer to trigger new instances - but my advice would be to try it first.

Hope that helps

Upvotes: 1

Related Questions