Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug MediaElement not playing correctly. SL3 Beta 1
2 replies. Latest Post by advcoder on May 4, 2009.
(0)
advcoder
Member
0 points
2 Posts
04-29-2009 8:36 PM |
I am loading a MediaElement dynamically.
public Sound(string name, byte[] buffer, Panel layoutRoot) { _name = name; MemoryStream ms = new MemoryStream(buffer); me = new MediaElement(); me.AutoPlay = false; me.SetSource(ms); layoutRoot.Children.Add(me); }
The buffer is loaded in by something like...
_parentAssembly.GetManifestResourceStream(fileName);
When I play the media element, it works just fine...the first time. The second time the element will not play the last bit of the sound. So in my game, I have the MouseEnter event set to play this dynamically created element, but the next time I play the sound through, it plays for only a portion of the mp3's full length.
A workaround I have done in the past was to just destroy/recreate a new MediaElement every time. Not pretty, but it worked. However, if I fire the audio off too quickly (I go over the buttons really fast), I get an error:
Error:Sys.InvalidOperationException: MediaError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR "
Why would I get an AG_E_NETWORK_ERROR if I am using a byte array with an internal memorystream?
StefanWick
Contributor
2864 points
438 Posts
05-01-2009 1:14 AM |
I did a quick test, but wasn't able to repro this. Do you have a site up for testing/debugging?
Does it repro for any audio file? Did this work for you in SL2?
Thanks, Stefan Wick
05-04-2009 12:36 PM |
Which problem do you want demo'd. not sure which one you tested?
As for the Media Element only playing correctly the first time through, you only need to take a small audio file, read the raw bytes into a memory stream and load the source. Play a few times. You will hear the last little bit being truncated. This was happening in SL2, and is still happening in SL3. The only workaround is to dispose of the mediaelement and create another one using the same byte array (as in, new MemoryStream, new MediaElement)
1 // This is the contstructor2 public Sound(byte[] buffer, Panel layoutRoot)3 {4 ms = new MemoryStream(buffer);5 me = new MediaElement();6 me.AutoPlay = false;7 me.SetSource(ms);8 layoutRoot.Children.Add(me); 9 }
I have done this on WMA and MP3 files in SL2, but only on a few MP3 files in SL3. All are usually less than a second in duration.
The error that pops up I can get rid of by doing an empty javascript method for the onerror, but it doesn't change the fact that it is there. So if you have your browser stop on all JS errors, you will probably see it. Easiest way (see above code) is to initialize audio with that method, and then fire an event to play it (mouseover on a button) then just go crazy mousing over it. There is one key piece of information though. You have to create a new sound after every event fires. (destroy the old and then create a new Sound) It shouldn't make a difference, as it is coming from a static byte array, but I still get the network error sometimes.
Edit: if you want the source code I am using, I can send it to you...but I can't post a lot of the code in here due to NDA restrictions. This has been a problem I have reported several times in previous versions of SL when I was building Tunnel Trouble/Zombomatic 3000.