
Play sound code#
If(dialog.ShowDialog() = DialogResult.OK)ĥ.Add the following method code under the button1_Click event hander. OpenFileDialog dialog = new OpenFileDialog() ĭialog.Filter = "Audio Files (.wav)|*.wav" This code displays the File Open dialog box and passes the results to a method named "playSound" that you will create in the next step.
Play sound windows#
To play an Audio file in the Windows form using C# let's check simple example as follows :ġ.Go Visual Studio(VS-2008/2010/2012) -> File Menu -> click New Project.Ģ.In the New Project -> click Windows Forms Application -> Give Name and then click OK.Ī new "Windows Forms" project will opens.ģ.Drag-and-Drop a Button control from the Toolbox to the Windows Form.Ĥ.Double-click the button to create automatically the default Click event handler, and add the following code. (How to play sounds you can see in other answers.)

In your application, read sound file path or DLL resource from your registry keys and play it.Create sound profile of your application in the Windows Registry (Hint: no need of programming, just add the keys into installer of your application.).Each user profile can override these sounds in own way.This way users can easily change or remove sounds from your application and you do not need to write any user interface for this – it is already there.Implement any other sounds as customizable by your users in Sound control panel Methods of class will play them for you. Question - play with questions (system message box window plays this one)Įxclamation - play with excalamation icon (system message box window plays this one)Ĭritical stop ("Hand") - play with error (system message box window plays this one) Use five standard system sounds in typical scenarios, i.e.Īsterisk - play when you want to highlight current event Besides that, always note these two points: Technical details of playing particular sound were provided in other answers. The following example uses the My. method to play a system sound.This is a bit high-level answer for applications which want to seamlessly fit into the Windows environment. The system sound Asterisk generally denotes errors.

The My. method takes as a parameter one of the shared members from the SystemSound class. Use the My. method to play the specified system sound. In the code snippet picker, it is located in Windows Forms Applications > Sound. The preceding code example is also available as an IntelliSense code snippet. The following example stops a sound that is playing in the background. In general, when an application plays a looping sound, it should stop the sound at some point. Use the My. method to stop the application's currently playing background or looping sound. Stopping the Playing of Sounds in the Background In general, when an application plays a looping sound, it should eventually stop the sound. When using this example, you should ensure that the application resources include a. In the following example, the My. method plays the specified sound in the background when PlayMode.BackgroundLoop is specified. When using this example, you should ensure that the file name refers to a.

In the following example, the My. method plays a sound. wav sound file that is on your computer Sub PlayBackgroundSoundFile() When AudioPlayMode.WaitToComplete is specified, My. waits until the sound completes before calling code continues.

You can also play a sound and wait for it to complete. The My. method allows the application to play only one background sound at a time when the application plays a new background sound, it stops playing the previous background sound. Playing Soundsīackground playing lets the application execute other code while the sound plays. The My.Computer.Audio object provides methods for playing sounds.
