Mike Hodnick's Blog

Workaround for "Short Sound" Silverlight Bug

Silverlight 2 has a bug with very short audio files (e.g. less than 1s). When you need to play a short audio file more than once, the beginning of the audio is clipped off each time after the first time it is played.

The code below is an example of how to reproduce the problem. The only way you can play audio in Silverlight is with the MediaElement class. The code below adds a MediaElement that plays a sound to a container (e.g. a user control or control such as a Canvas). The code also contains a method that we can assume is called many times:

MediaElement media;
void Load()
{
  media = new MediaElement();
  media.Source = new Uri("click.mp3", UriKind.Relative);
  media.AutoPlay = false;
  this.Children.Add(media);
}

void HandleMouseEnter(object sender, MouseEventArgs e)
{
  media.Stop();
  media.Play();  
}

This code results in a clipped audio sound at the beginning of the audio file.

The problem appears to be with adding the MediaElement only once to the container. To get around this problem, you can add a MediaElement to the container each time you want to play the sound. It is highly inefficient code, but addressed the clipped audio issue:

void HandleMouseEnter(object sender, MouseEventArgs e)
{
  MediaElement media = new MediaElement();
  media.Source = new Uri("click.mp3", UriKind.Relative);
  media.AutoPlay = false;
  this.Children.Add(media);
  media.Play();
  this.Children.Remove(media);
}

Hopefully this issue will be resolved in Silverlight 3.

Technorati Tags: ,