How to play simultaneous multiply audio sources in Silverlight

Posted by Shurup on Stack Overflow See other posts from Stack Overflow or by Shurup
Published on 2010-05-05T16:26:21Z Indexed on 2010/05/05 16:28 UTC
Read the original article Hit count: 302

I want to play simultaneous multiply audio sources in Silverlight. So I've created a prototype in Silverlight 4 that should play a two mp3 files containing the same ticks sound with an intervall 1 second. So these files must be sounded as one sound if they will be played together with any whole second offsets (0 and 1, 0 and 2, 1 and 1 seconds, etc.)

I my prototype I use two MediaElement (me and me2) objects.

DateTime startTime;

private void Play_Clicked(object sender, RoutedEventArgs e) {
  me.SetSource(new FileStream(file1), FileMode.Open)));
  me2.SetSource(new FileStream(file2), FileMode.Open)));
  var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1) };
  timer.Tick += RefreshData;
  timer.Start();
}

First file should be played at 00:00 sec. and the second in 00:02 second.

void RefreshData(object sender, EventArgs e) {
    if(me.CurrentState != MediaElementState.Playing) {
      startTime = DateTime.Now;
      me.Play();
      return;
    }

    var elapsed = DateTime.Now - startTime;
    if(me2.CurrentState != MediaElementState.Playing && 
      elapsed >= TimeSpan.FromSeconds(2)) {
      me2.Play();
      ((DispatcherTimer)sender).Stop();
    }
  }

The tracks played every time different and not simultaneous as they should (as one sound).

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about audio