Saturday, October 20, 2012

Silverlight XNA sound effect

standart

var laserStream =
    Application.GetResourceStream(
        new Uri("laser_shot.wav", UriKind.RelativeOrAbsolute));
 
var effect = SoundEffect.FromStream(laserStream.Stream);
effect.Play();



multiple instance


var engineStream =
    Application.GetResourceStream(
         new Uri("engine_rumble4.wav", UriKind.RelativeOrAbsolute));
 
_engineEffect = SoundEffect.FromStream(engineStream.Stream);
 
SoundEffectInstance engineInstance = _engineEffect.CreateInstance();
engineInstance.IsLooped = true;
engineInstance.Pitch = -1.0f;   // low sound
engineInstance.Volume = 0.75f;
engineInstance.Play();
 
// offset a second instance by 1 second, helps mask my bad loop point
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += (s, ea) =>
    {
        timer.Stop();
        SoundEffectInstance engineInstance2 = _engineEffect.CreateInstance();
        engineInstance2.IsLooped = true;
        engineInstance2.Pitch = -.75f;   // slightly higher sound
        engineInstance2.Volume = 0.5f;
        engineInstance2.Play();
    };
 
timer.Start();

No comments: