Play sound in VB.NET

New product

How to play many sounds at the same time in VB.NET without using external libraries. Pure NET Framework.

Play Sound in VB.NET with WinForms


Many times we want to reproduce sounds for our multimedia application made in VB.NET. The NET, brings natively for its two windows systems two audio engines that allow us to do this work without using external references.

For programs developed in Windows Forms or WinForms, we have the SoundPlayer class.

For the programs developed in WPF, we have the MediaPlayer class.

For the first (Sound Player), class allows us to play sound stored in files and in data stream. Something that is very interesting if we want to load sounds in a memory buffer. The unfavorable point is that it does not allow the simultaneous reproduction of sounds, that is, it does not mix sounds. That is to say, if we have a voice narrating something and we want to reproduce some button press sound, the latter will not be heard.

The second (MediaPlayer), class allows us to play sound saved in resource files of the WPF program. It is not possible to load them from a data stream which is your unfavorable point, it is not compatible with WinForm. The good point is that MediaPlayer allows us to mix sounds and then we can play several sounds at the same time and all will be heard.

Considering this, it is possible to use the WPF MediaPlayer in the WinForms project, but without using the problematic hosting system.

How to reproduce multiple sounds in VB.NET with WinForms?

Advertising.

To play many sounds at the same time in VB.NET:

1 - We create a dynamic link library and import the references of the assemblies: WindowsBase, System.Core

2 - We import the assemblies: System.Windows.Media to a class for audio control.

3 - Create a class that has the following structure:

Code:

'''
''' Controla la pieza de sonido que entra al buffer de dispositivo.
'''

'''
Public Class PiezaSnd

Private WithEvents mp As MediaPlayer
Private RecursoSnd As String
Private
_Reproduciendo As Boolean
Private
_Pausa As Boolean

Public Sub New
(ByVal NombreSnd As String)
mp =
New MediaPlayer
RecursoSnd = NombreSnd
_Reproduciendo =
False
_Pausa = False
End Sub

Public ReadOnly Property
Reproduciendo As Boolean
Get
Return
_Reproduciendo
End Get
End Property

Public Sub
Open()
Dim Path As String
Path = Directory.GetCurrentDirectory() & "sonidos" & RecursoSnd

mp.Open(
New Uri(Path))
_Reproduciendo =
False
_Pausa = False
End Sub

Public Sub
Pause()
mp.Pause()
_Reproduciendo =
False
_Pausa = True
End Sub

Public Sub
Play()
If Not _Pausa Then Open()
mp.Play()
_Reproduciendo =
True
_Pausa = False

End Sub

Public Sub
[Stop]()
mp.Stop()
_Reproduciendo =
False
_Pausa = False

End Sub

Public Sub
Close()
mp.Stop()
mp.Close()
_Reproduciendo =
False
_Pausa = False
End Sub

Private Sub
mp_Changed(sender As Object, e As EventArgs) Handles mp.Changed
_Pausa =
False
End Sub

Private Sub
mp_MediaEnded(sender As Object, e As EventArgs) Handles mp.MediaEnded
_Reproduciendo =
False
End Sub

Private Sub
mp_MediaFailed(sender As Object, e As ExceptionEventArgs) Handles mp.MediaFailed
_Pausa =
False
End Sub

Private Sub
mp_MediaOpened(sender As Object, e As EventArgs) Handles mp.MediaOpened
_Pausa =
False
End Sub
End Class

4 - Compile the library and link it to the WinForms project. And ready.

Advertising.

Considerations when using the WPF MediaPlayer


Make sure to always open the audio resource before executing it, usually when going from Play to Stop the WPF downloads the data buffer to save memory. Then the sound will not be played in the next Play order.

Keep in mind that each loaded sound consumes a lot of memory, think about creating a library of sounds, so that they are loaded only in the necessary amount. Do not load sounds by objects.

It's all at the moment, so Play Sound in VB.NET using WPF is very simple and very powerful.

Written by:

Eliminate G.

The Program

03/21/2018
Grade 
03/22/2019

Excelente

Gran post

    Grade 
    03/21/2019

    buen aporte

    Muy buena tu idea del mezclador de sonidos.

      Write your review!

      Write a review

      Play sound in VB.NET

      Play sound in VB.NET

      How to play many sounds at the same time in VB.NET without using external libraries. Pure NET Framework.

      Ask a question

      NO registration required!

      If the question you have has not yet been answered here, use the form below to ask something about this addon.

      (optional)
      *(Required to be notified when an answer is available)

      If the download link redirects to another product that is not described in the article or is broken, report it using our Reporting Form