Using Silverlight 2 for Short Audio Caching
I am trying to use a large number of short sound samples in a game I am creating in Silverlight 2. The samples are less than 2 seconds long.
I would prefer to load all audio sequences onto the canvas during initialization. I've added a media item to the canvas and a shared list to manage it. So far, it works.
When I play the sample for the first time, it plays fine. If it's finished playing and I want to reuse the same element, it cuts off the first part of the sound. To play the sample again, I pause and play the media item.
Is there any other method I should use with samples so that the audio is not clipped and good performance is achieved?
a source to share
Also, it's probably a good idea to make sure all of your audio sequences are output to the client side first. Depending on how you set it up, it is possible MediaElements are using their progressive download functionality to fetch media from the server. While there is nothing wrong with that (browser caching should help you after the initial load), it means you have to deal with the browser cache and there are some potential issues.
Possible actions:
- Mark your audio files as "Content". This will force them to be pinned to .xap.
- Load the audio files into MemoryStreams (see Application.GetResourceStream method) and call MediaElement.SetSource ().
NTN, Erik
a source to share
Some comments:
From MSDN : Try to limit the number of MediaElement objects you have in your application at one time. If you have more than one hundred MediaElement objects in your application tree, whether they are playing at the same time or not, MediaFailed events may be raised. A way to get around this is to add MediaElement objects to the tree as needed and remove them when they are not.
You can try to find the start of the pattern before resetting the current point to reuse with:
mediaelement.Position = new TimeSpan();
See also MSDNs MediaElement.Position .
a source to share
One technical tool you can use, although I'm not sure how well it will work in Silverlight, it creates one big file with all of your samples concatenated together (maybe half a second of silence between them). Find out the timecode for each sample and find the media item at that position and play. You only need as many media elements as there are simultaneous sounds you want to play.
a source to share