Search code examples
.netvb.netnaudio

Sending directly from a WAV file to a DirectShow Sound Card


I am using VB.net to integrate sending a wave file to a specific speaker (exactly what is done in the sample file). But, I am having trouble decoding what is actually done in the sample file because what I am looking for is much more simple.

What is the code to send a WAV file to a speaker how do I open and decode said wave file?


Solution

  • I figured it out simpler then I originally thought. Figured I would provide my own answer to help someone else looking for the same information!

    Imports NAudio
    

    Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim wavefile As New NAudio.Wave.Mp3FileReader("C:\Users\iqm2admin\Desktop\Pause.mp3") 'The Audio MP3 file you want to play
        Dim waveout As New NAudio.Wave.WaveOut() 'Create Wave Object
        Dim devicecount As Integer = NAudio.Wave.WaveOut.DeviceCount() 'Count the devices
        Dim TestString As String = "Speaker description" 'The desciption of the speaker you want to play to
        Dim SDevice As String = Nothing
        Dim DeviceNum As Integer
        For i As Integer = 0 To devicecount - 1
            SDevice = NAudio.Wave.WaveOut.GetCapabilities(i).ProductName
            If TestString.ToLower = SDevice.ToLower Then                   'Find the speaker you want to play to
                DeviceNum = i
            End If
        Next
        waveout.DeviceNumber = DeviceNum ' set the device to play to
        waveout.Init(wavefile)
        waveout.Play() 'play the file
    End Sub
    

    End Class