Welcome to MSDN Blogs Sign in | Join | Help

Gilles' Thoughts

A random collection of tidbits

Tags

No tags have been created or used yet.
Playing back Wave files in Silverlight

I’ve been working on Silverlight for a couple of years now, and finally decided to start talking about it in my blog. With the recent beta, we’ve added raw A/V support to Silverlight 3, and that means not only that people will be able to write their own decoders for Silverlight, but also that they can now play back PCM audio through managed code. The easiest way to get PCM data is simply to use a Wave file. Wave files are a container format for multiple audio encodings, so not all files will play back in Silverlight, but the PCM encoded ones will. To help out, I’ve published a sample MediaStreamSource implementation on Code Gallery (http://code.msdn.microsoft.com/wavmss).

The easiest way to try out the code is to open a local wave file from your drive using the OpenFileDialog (C:\Windows\Media has a bunch of little sound bites).

private void Button_Click(object sender, RoutedEventArgs e) 
{
    OpenFileDialog ofd = new OpenFileDialog(); 

    if (ofd.ShowDialog().Value)
    {
        Stream s = ofd.File.OpenRead();
        WaveMediaStreamSource wavMss = new WaveMediaStreamSource(s);
        try
        {
            me.SetSource(wavMss);
        }
        catch (InvalidOperationException)
        {
            // This file is not valid
        }
    }
} 

This snippet will play the wave file right away. Remember that to get the FileOpenDialog, you need to hook the handler to a user initiated action. An easy way for this, is to put a MediaElement and a button on a page.

<UserControl x:Class="SampleWave.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <MediaElement Name="me"/>
        <Button Name="b" Width="200" Height="20" Content="Open" Click="Button_Click"/>
    </Grid>
</UserControl>

That’s it, you can now play wave files.

Posted: Monday, March 23, 2009 11:32 AM by gillesk

Comments

AlexandreMutel said:

Hi Gilles,

This is a great and simple sample to explain the new RAW audio pipeline in SL3.

I'm very excited about this feature as it opens a wide range of new realtime audio-visual experience!

Although, the main issue right now with the RAW audio is the high audio latency and then doesn't allow to make realtime audio performance, as it is reported here : http://silverlight.net/forums/p/82270/192635.aspx

It seems that the team working on the MediaStreamSource is aware of this problem, hope that a correction will be possible for the final release of SL3!

# March 24, 2009 7:48 AM

gillesk said:

Thanks for the feedback. We're aware of the latency issue and should address it for the release. I don't know how low we'll be able to go (with lower the latencywe need to get the data from the app mre frequently).

# March 24, 2009 12:31 PM

mliebster said:

Thanks for posting the code for this!

We currently are converting our Windows .WAV files to WMA on the server on demand and then playing them via SL2.

Unfortunately some of our audio files are in a Dialogic PCM muLaw format. So these go through 2 conversions to be played - first be converted to wav and then converted to WMA.

This will simplify things so much!

# April 21, 2009 3:36 PM
Anonymous comments are disabled
Page view tracker