none
best option for playing a sound?

    Question

  • Trying to find the best option to play a sound.

    Right now when I need to play on i have code that gets embedded in a hidden span tag. This works find in IE as it will use WMP to play the sound. If I use firefox or a person has  different plug in for playing media (like quick time) this option does not work. Is there a better (and easier way) to do this in Silverlight? 

    Monday, January 21, 2008 3:21 PM

Answers

  • Try it with out the Visibility="Collapsed", since there is no visual component to your media file the MediaElement won't render anything visually.  Plus I believe that's causing your mediaElement to not even be rendered (or in this case more appropiately added to object tree) so it can't play.  It also depends on where your JS code is running, but I'm guessing its in the Loaded event handler.  To test the script code you could also remove, just to make sure the file is playing correctly AutoPlay="False".

    Tuesday, January 22, 2008 11:16 AM

All replies

  • This is pretty easy using Silverlight. All you need is to create a Silverlight application and create a MediaElement and set the source property of the element to the URI of your sound file. Remember ti set AutoPlay property to false, otherwise the sound file will be played automatically. Whenever you need to play the sound invoke the play method of the MediaElement.
     

    Monday, January 21, 2008 3:34 PM
  • and this will work no matter what the user's browser is set to use?

    Monday, January 21, 2008 4:40 PM
  • It will work as long as Silverlight is installed and the browser is supported. Currently Silverlight is available for IE, FireFox on windows, and Safari, Firefox on mac. For more information on the supported operating systems and browsers check this page http://www.microsoft.com/silverlight/system-requirements.aspx#4_5 

    Please also note that Silverlight requires SSE instruction set, so it will not work on PII nor old AMD Athlon processors. 

    Monday, January 21, 2008 5:20 PM
  • having trouble here, I am not getting any errors yet my sounds don't play.

    <MediaElement x:Name="WrongSound" Visibility="Collapsed" AutoPlay="False" Volume="1" Source="sounds/Loser3.wav"></MediaElement>

    JS side

    sender.findName("WrongSound").stop();

    sender.findName("WrongSound").play();

    I'm sure its something stupid.

    Monday, January 21, 2008 6:38 PM
  • WAV files are not a supported format.  The list of supported formats is available here: http://msdn2.microsoft.com/en-us/library/bb412386.aspx

    Tuesday, January 22, 2008 2:43 AM
  • odd, I thought that might be the issue and converted on of the wav files to an mp3 which did not solve the issue.

    I just tried an mp3 song and it's still a no go.

    Tuesday, January 22, 2008 7:01 AM
  • Try it with out the Visibility="Collapsed", since there is no visual component to your media file the MediaElement won't render anything visually.  Plus I believe that's causing your mediaElement to not even be rendered (or in this case more appropiately added to object tree) so it can't play.  It also depends on where your JS code is running, but I'm guessing its in the Loaded event handler.  To test the script code you could also remove, just to make sure the file is playing correctly AutoPlay="False".

    Tuesday, January 22, 2008 11:16 AM
  • ok I got it working EXCEPT. they only play once. If I attempt to call the sounds a second time, they will not play. I have attempted to add a .stop() later on but that didnt help.

    Tuesday, January 22, 2008 12:58 PM
  • Are you just using AutoPlay or are you calling play in code?  You'll have to post your code if you want help with that.

    Tuesday, January 22, 2008 3:34 PM
  • I got it!

    Tuesday, January 22, 2008 6:10 PM
  • Wait....SL doesn't play wav files natively??? 

    If I want to create an interface to play wav files from within ASP.NET/Silverlight, what should I use? 

    Wednesday, February 27, 2008 2:40 PM
  • And there are millions of wav sound effects and I don't have a decent converter for free anywhere.  GREAT !

    Now with this :

    MediaElement m = new MediaElement();
    m.Source =
    new Uri(Application.Current.Host.Source + "../Sounds/test.mp3");
    m.Position =
    new TimeSpan(0);
    m.Play();

    There are no errors at all but no sound is coming out of my speaker.  It's a total silence.

    Tuesday, October 07, 2008 8:16 PM
  • m = new MediaElement();
    m.Source =
    new Uri(Application.Current.Host.Source + "../Sounds/test.mp3");
    m.Position =
    new TimeSpan(0);
    m.Play();

    There are no errors at all but no sound is coming out of my speaker.  It's a total silence.

     

    I have the same problem with about 1/2 of my sound effects.  When I set a break point in this code and step through it, the sound plays, but at run time, it does not play.  For other sounds, using the same code, it plays correctly.

     Could this be related to the duration of the sound file or the amount of clock_cycles spent in the method invoking the sound?

     

    EDIT: After more tinkering, we were able to get all sounds to play by adding a MediaCompleted Event handler to every sound, and waiting until the sound is complete before continuing down the code path.  This may not be a viable solution for longer sounds, but for our (.2) second duration sound effects, it works fine.  Of course, this is a painful (and less readable) way of coding....

    Friday, October 24, 2008 10:38 AM
  • I'm still unable to play a sound

    MediaElement m = new MediaElement();
    m.Source =
    new Uri(Application.Current.Host.Source + "../Sounds/test.mp3");
    m.Visibility =
    Visibility.Collapsed;
    m.Position =
    new TimeSpan(0);
    m.Play();

    Can it be the .mp3 format I have ?.
    I'm not an expert on sound so is there any Web site which offers SilverLight sound effect for mouse over or anything like this ?

    Can't wait to ear something guys :)

     

    Saturday, October 25, 2008 4:14 PM
  • Maybe this is your prob...... 

    sender.findName("WrongSound").stop();

    sender.findName("WrongSound").play();

    should be

    sender.content.findName("WrongSound").stop();

    sender.content.findName("WrongSound").play();

    Regards

    Marc.

    Saturday, October 25, 2008 8:49 PM
  • Hi,

    Not really.  The code I wrote above is all you need to play a sound that take the sound where it is on the server as I already do for many images on the site so I'm using a MediaElement, I'm getting the sound I would do for an image and I play it when the user hover over a button.

    But the sound never play.

    Its an .mp3 I downloaded on the internet and I never found any suitable sound effect in wma format which SilverLight work best with !
    So either the .mp3 is not really recognize by SilverLight or there's something else I don't know but I don't really understand why the sound do not play since there's no error at all and this code goes on like butter on toast.

     

    Sunday, October 26, 2008 5:30 PM
  • God now I feel stupid.  I was trying to play a sound directly. 

    Nah I forgot to add the sound to the LayoutRoot.Children then play the sound by FindName(...)

    now this is working well,

    ((MediaElement)this.LayoutRoot.FindName("Audio")).Stop();
    ((
    MediaElement)this.LayoutRoot.FindName("Audio")).Play();

    Sunday, October 26, 2008 6:42 PM
  • Strange tho.  I have 2 littles wma file.  One plays well, repeatitively and the other plays once and finished with a weird burk then it doesn't play anymore
    Both sounds are the same length, small, but well there's no explaination why one works and the other don't

     

    Sunday, October 26, 2008 6:53 PM
  • Ok now I think I see what's going on.

    First time the sound is played, it plays well.  then each next time to try to play it it sound like it plays just a part of it like half of your sound either the begining or the end :)

    You can see what it sounds like at http://gearworld.dyndns.org:8082/devpreview before 9:00PM
    Mouve your mouse over the ORANGE BALL on the UPPER RIGHT corner
    then move away and go back on :)

     

    Sunday, October 26, 2008 7:07 PM
  • How funny things are with .wma format.
    I'm able to play the sound with MediaElement from SilverLight and unable to play it in Windows Media Player 11 NOR Zune software
    they play it but no sound comes out of it.

    WEIRD !!!!!!!

     

    Sunday, October 26, 2008 7:53 PM
  •  The MediaElement in silverlight is super bugged.  I've found that sounds under 1 second in length wont reliably play more than once.  My solution has been to make sure all my sounds are 1 second in length...even if that means padding it with silence Sad

    Friday, March 20, 2009 11:24 AM