-1

All my sound clips are in mp3, so I do not really feel like translating them tp wav.
If I try to play a mp3 file I get a exception, but the code works fine for .wav files. I figure there must be a way to play mp3 files.

Stream s = TitleContainer.OpenStream("sounds/bag.mp3");

// throws a exceptio if its a mp3 file
SoundEffect effect = SoundEffect.FromStream(s);
FrameworkDispatcher.Update();
effect.Play();
pinckerman
  • 4,115
  • 6
  • 33
  • 42
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

2 Answers2

2

It is by design - SoundEffect.FromStream only works with wave files. As per MSDN:

The Stream object must point to the head of a valid PCM wave file. Also, this wave file must be in the RIFF bitstream format.

The audio format has the following restrictions:

  • Must be a PCM wave file
  • Can only be mono or stereo
  • Must be 8 or 16 bit
  • Sample rate must be between 8,000 Hz and 48,000 Hz
Den
  • 16,686
  • 4
  • 47
  • 87
  • You can. For example, if you use Silverlight+XNA you can use `MediaElement`. Or, if you work on a Windows Phone 8 project, you can use native code to do that via XAudio2. – Den Mar 19 '13 at 02:53
0

Try to use Media Element..

MediaElement MyMedia = new MediaElement();
MyMedia.Source = new Uri("sounds/bag.mp3", UriKind.RelativeOrAbsolute);
MyMedia.Play();
pinckerman
  • 4,115
  • 6
  • 33
  • 42
Belal Ghanem
  • 97
  • 1
  • 11
  • MediaElement is VERY buggy and not intended for this kind of use. The sound should be in a supported format and should be played through the XNA library – DotNetRussell Dec 20 '13 at 17:50