SIGN UP MEMBER LOGIN:    
ARTICLE

SoundPlayer in WPF

Posted by Mahesh Chand Articles | WPF with C# July 29, 2008
The code snippet in this article shows how to play sounds in WPF.
Reader Level:
Download Files:
 

WPF uses Win32 or Windows Forms SoundPlayer and MediaPlayer controls to play sounds. The following code snippet and attached source code shows how to use it in WPF.

private void BrowseButton_Click(object sender, RoutedEventArgs e)

{

    OpenFileDialog dlg = new OpenFileDialog();

    dlg.InitialDirectory = "c:\\";

    dlg.Filter = "Sound files (*.wav)|*.wav|All Files (*.*)|*.*";

    dlg.FilterIndex = 2;

    dlg.RestoreDirectory = true;

 

    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)

    {

        string selectedFileName = dlg.FileName;

        FileTextBox.Text = selectedFileName;

        MediaPlayer mp = new MediaPlayer();

        mp.Open(new Uri(selectedFileName, UriKind.Relative ));

        mp.Play();

    }

}

Note: By default a WPF application does not have reference to the System.Windows.Forms namespace and assembly. You need to add references to the System.Windows.Forms.dll and also import the namespace by adding the following line of code to your class:

using System.Windows.Forms;

 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor