Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use for playing sound effects in silverlight for wp7

I know I can reference XNA for the SoundEffect class and that's what I've been doing so far but I was wondering if there was a better way than what I've been doing.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

using (var stream = TitleContainer.OpenStream("test.mp3"))
{
          var effect = SoundEffect.FromStream(stream);
          FrameworkDispatcher.Update();
          effect.Play();
}

For my test app I have 20 sounds each 1 second long that I want to play once button are pressed. I'm playing around with different techniques but if possible I'd like to know how professionals go about doing this before I commit in making a sound effect based app. Little things such as loading the sound effect first or loading it the instance the button is pressed would be helpful.

Thanks.

like image 563
9 revs Avatar asked Oct 10 '22 20:10

9 revs


2 Answers

If I were you I would use PhoneyTools SoundEffectPlayer

This class is used to play SoundEffect objects using the XNA integration. The player must live long enough for the sound effect to play so it is common to have it scoped outside a method. For example:

public partial class MediaPage : PhoneApplicationPage
{
  // ...

  SoundEffectPlayer _player = null;

  private void playButton_Click(object sender, RoutedEventArgs e)
  {
    var resource = Application.GetResourceStream(new Uri("alert.wav", UriKind.Relative));
    var effect = SoundEffect.FromStream(resource.Stream);
    _player = new SoundEffectPlayer(effect);
    _player.Play();

  }
}
like image 126
Lukasz Madon Avatar answered Oct 25 '22 10:10

Lukasz Madon


I think a good example would be the official sample on AppHub. It demonstrates how to play multiple sounds. You can directly download the sample from here.

This sample demonstrates how to use the XNA Framework's SoundEffect and SoundEffectInstance classes to play multiple sounds simultaneously in a Silverlight application for Windows Phone. It also shows a simple way to set up a DispatchTimer to call FrameworkDispatcher.Update in order to simulate the Game loop for the XNA Framework's internals. Finally, it shows how to load a wave audio file into a Stream that can be played by the SoundEffect classes.

like image 35
keyboardP Avatar answered Oct 25 '22 09:10

keyboardP