1

I am using VS2010,C# to develop a Silverlight web based game. I want to play mp3 and this is my existing code:

StreamResourceInfo sri = Application.GetResourceStream(new Uri("/TennisSL;component/Images/idle.mp3", UriKind.Absolute));
m.SetSource(sri.Stream);
Scene.Children.Add(m);
m.Play(); // Plays the sound

The above code does not play as no sound is heard! What could be wrong? Is there anything missing?

This is my MediaElement object declaration at the top of program:

MediaElement m = new MediaElement();
Oak Bytes
  • 4,649
  • 4
  • 36
  • 53
Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115
  • Silverlight seems to be able to support MP3s, see http://msdn.microsoft.com/en-us/library/cc189080%28v=vs.95%29.aspx. Try adding an event handler to the MediaElement's `MediaFailed` event. If this event handler gets called, what's the exception in the `ExceptionRoutedEventArgs` parameter? – Luke Woodward Mar 24 '12 at 13:58
  • thanks, I test it, I've also used MediaElement tag in my XAML, but there is still no luck! – Ali_dotNet Mar 24 '12 at 14:01

1 Answers1

2

Add the following code :

m.Position = new TimeSpan(0,0,0);

then :

m.Play();

Thay way, you put the stream back to the start position and you can play the sound.

Oliver
  • 807
  • 2
  • 12
  • 23