locked
Playback of small/short sounds with MediaElement? RRS feed

  • Question

  • I have a problem with the "pop" sound in my game Balloon Mayham. http://balloon.laumania.net/

    On my machine the "pop" sound when you pop a balloon, doesn't always sound right. Sometimes it's just like 10% of the sound you hear or something.

    Any ideas why this happen? I have search alittle and have seen that other have the same problem, but didn't find a solution.

    I have an MediaElement in my xaml of course. I load in the mp3 like this:

     

    StreamResourceInfo sri = Application.GetResourceStream(new Uri("Media/baloon_pop.mp3", UriKind.Relative));
    PopSoundMediaElement.AutoPlay = false;
    PopSoundMediaElement.SetSource(sri.Stream);

     
    I play the sound like this:

     

    PopSoundMediaElement.Stop();
    PopSoundMediaElement.Play();

      

    Any ideas?

    Thanks in advance,

    Qbus

    Thursday, June 11, 2009 3:50 PM

Answers

  • I now changed the kbps of the mp3 file from 128 to 64. Now it's working, or at least on my machine the "sound bug" doesn't happen very often anymore.

    Seems like SL can't handle a file that small (under 1 second), with a high kbps. But actually when the sound is that short, you can't hear the difference anyways :)

    Please try out my game and report if the problem is gone on your machine too:

    http://balloon.laumania.net/

    Remember to clear your browser cache, so you are sure you have the newest version

    Thanks in advance Big Smile

    Saturday, June 13, 2009 4:30 PM

All replies

  • Obus I can only offer that my code seems nearly identical to yours:

    StreamResourceInfo sri = Application.GetResourceStream(new Uri("Audio/explode.mp3", UriKind.Relative));

    m_Media_Explosion.SetSource(sri.Stream);

    m_Media_Explosion.AutoPlay = true;

    m_Media_Explosion.Play();

    But there's only one difference: I don't call Stop() first. I've never noticed any sound problems so far. I remember fussing with Stop() and AutoPlay early on, because I'd have spurious sound starts when reinitializing the custom controls that had MediaElement sounds. But since I started declaring them with Source="" like this: 

    <MediaElement x:Name="m_Media_Explosion" AutoPlay="False" Source=""/>

    I haven't had to call Stop(). Good luck!

    Thursday, June 11, 2009 4:49 PM
  • Thanks for your reply. Yeah the way we do it seems pretty similar. Are you using SL2 or SL3? I'm currently using SL2.

    I not sure why I do the Stop(), but I think there is a reason. As I recall it, it's somehow for resetting the sound. I'll try to remove and see if that helps, sadly I don't think so.

    Have you tryed out my game? Do you hear the problem I'm having when your playing? Is your game sounds played as often as main?

    Qbus

    Friday, June 12, 2009 2:39 AM
  • The only way I've had this work consistently is to call SetSource before EVERY Play call. To make things abit more efficient, I read the original stream into a byte array and then create a MemoryStream off of that which gets passed into SetSource.

    Friday, June 12, 2009 7:59 AM
  • Qbus, I'm using Silverlight 2.0, like you. I played your game and I do detect an oddity with the sound. However, I am not sure that it's duration--sounds more like attentuated volume.

    I get 2 or 3 good loud pop sounds. Then, the pop sounds start getting harder to hear. But I don't think they're starting late or ending early--I think it's the whole sound timeline but diminished volume. Odd. Every time I refresh the browser to start a new game, I get 2 or 3 good loud pops again.

    Friday, June 12, 2009 11:32 AM
  • Qbus, I'm using Silverlight 2.0, like you. I played your game and I do detect an oddity with the sound. However, I am not sure that it's duration--sounds more like attentuated volume.

    I get 2 or 3 good loud pop sounds. Then, the pop sounds start getting harder to hear. But I don't think they're starting late or ending early--I think it's the whole sound timeline but diminished volume. Odd. Every time I refresh the browser to start a new game, I get 2 or 3 good loud pops again.

     

    Thats exactly the experience I have too. I can't figure out what I'm doing wrong, the sound part isn't that complicated Big Smile

    Friday, June 12, 2009 1:32 PM
  • The only way I've had this work consistently is to call SetSource before EVERY Play call. To make things abit more efficient, I read the original stream into a byte array and then create a MemoryStream off of that which gets passed into SetSource.

     

    Hmm, I'll just tryed that, but didn't work.

    I make this method:

    private void PlayPopSound()
    {
                StreamResourceInfo sri = Application.GetResourceStream(new Uri("Media/baloon_pop.mp3", UriKind.Relative));
                PopSoundMediaElement.SetSource(sri.Stream);
                PopSoundMediaElement.Play();
    }

    It doesn't work, actually this way I'm not hearing any sound at all.

    Is this a know "bug" in SL2 or can anyone explain why this is happening? I can't be the first to have this issue.

    Thanks for your feedback so far guys Yes

    Friday, June 12, 2009 1:43 PM
  • QBus, I've discovered that one of my game sounds, the shortest one, is also too quietly played by SL2 (as opposed to how they sound in Windows Media Player). I'm half-tempted to append a half-second of silence onto that shortest sound to see if sound duration is part of the issue.

    Saturday, June 13, 2009 11:02 AM
  • QBus, I've discovered that one of my game sounds, the shortest one, is also too quietly played by SL2 (as opposed to how they sound in Windows Media Player). I'm half-tempted to append a half-second of silence onto that shortest sound to see if sound duration is part of the issue.

     

    I tryed to put in some silence, as you suggested and it actually helped a little. The sound bug wasn't gone, but it were better. The really sad thing is the my mp3 went from 5kb to 20 kb Sad I don't want that.

     

    I googled a little and apparently it works for this guy in his scenario. I can hit the "Play sound" button very quick and the sound is good everytime.

    http://silverlight.net/blogs/msnow/archive/2008/07/25/tip-of-the-day-22-how-to-add-sound-effects-music-and-video-to-your-silverlight-app.aspx

    I got some suggestions that it might be the bitrate or something on my mp3 file, that courses the problem. I'll look into it.

    Saturday, June 13, 2009 4:19 PM
  • I now changed the kbps of the mp3 file from 128 to 64. Now it's working, or at least on my machine the "sound bug" doesn't happen very often anymore.

    Seems like SL can't handle a file that small (under 1 second), with a high kbps. But actually when the sound is that short, you can't hear the difference anyways :)

    Please try out my game and report if the problem is gone on your machine too:

    http://balloon.laumania.net/

    Remember to clear your browser cache, so you are sure you have the newest version

    Thanks in advance Big Smile

    Saturday, June 13, 2009 4:30 PM
  • Qbus, it's better but not fixed (yes, I cleared browser cache). Before, I used to get only 2 or 3 full-volume pops and then all the rest would be very quiet. Now at least I get more than just the first couple pops at full-volume. There are normal loudness pops well into the game now. However, I also still get reduced-volume pops about half the time, quite randomly. Sometimes the pop is completely inaudible in fact, but the next pop might be normal loudness. Then another quiet one, etc. Gotta hate problems like this.

    Sunday, June 14, 2009 12:12 PM
  • Qbus, it's better but not fixed (yes, I cleared browser cache). Before, I used to get only 2 or 3 full-volume pops and then all the rest would be very quiet. Now at least I get more than just the first couple pops at full-volume. There are normal loudness pops well into the game now. However, I also still get reduced-volume pops about half the time, quite randomly. Sometimes the pop is completely inaudible in fact, but the next pop might be normal loudness. Then another quiet one, etc. Gotta hate problems like this.

     

    Damm... hate this problem! I can't see what I do wrong, as this part is very simple. Well, let's hope I come up with a solution at some point... it is "better" now after all :)

    Sunday, June 14, 2009 3:21 PM
  • I just had the honor and I finished #1 ;)

    When hitting 2 baloons in rapid succession, 1 gets skipped or distorted.

    I doubt this is something you have done wrong, it's probably some bug within the ME itself.

    This might be a longshot, but you could try using 2 Mediaelements in alternating succession,
    to give the predecessor time to "recover".

    Wednesday, June 17, 2009 4:28 PM
  • Yes that's what I do, I have a pool of media elements and only reuse them after they go to a "stopped" state.

    Wednesday, June 17, 2009 4:49 PM
  •  Hey we have a winner for today Big Smile Krasshirsch!

     Well, I have actually tryed using two MediaElements. Then when I called my "PlayPopSound" methods, I had a bool with I used to switch between the two MediaElements....sadly it gave the same result...

    Wednesday, June 17, 2009 4:51 PM