Search code examples
c#silverlightsilverlight-3.0mp3

How to load MP3 files from a FileReference in Silverlight?


How to load MP3 files from a FileReference in Silverlight? I'm looking for something like this in Silverlight


Solution

  • This is fairly easy to do.

    Here is a quick sample. Xaml:

    <StackPanel>
        <MediaElement x:Name="media" />
        <Button Content="Load MP3" Width="200" Click="Button_Click" />
    </StackPanel>
    

    And the c#:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Create an instance of the open file dialog box.
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
    
        // Set filter options and filter index.
        openFileDialog1.Filter = "MP3 Files (.mp3)|*.mp3";
        openFileDialog1.FilterIndex = 1;
    
        openFileDialog1.Multiselect = false;
    
        // Call the ShowDialog method to show the dialog box.
        bool? userClickedOK = openFileDialog1.ShowDialog();
    
        // Process input if the user clicked OK.
        if (userClickedOK == true)
        {
            // Open the selected file to read.
            System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
    
            media.SetSource(fileStream);
            media.Play();
        }
    
    }
    

    I'll let you add the output panel with the text about loading and such. :)