I am using visual studio / basic 2019. using the windows media to play sounds (WMPLib.WindowsMediaPlayer).
It works great, I can play multiple sounds etc. However what I realy want to do is play some sounds on sound card A. And other sounds in sound card B.
I am thinking that changing the default sound card, then playing the sound, then changing the defualt sound card and playing another sound might be the way to go, but I think when the sound card changes all the currently playing sounds stop.
Has anyone got any ideas how to acheieve this.

Thanks, certainly looks like its what I'm after, only problem is it looks like it c# rather than vb and its a complicated install, but I'll give it a go.

OK, got it all installed and using the following code:

filereader = New AudioFileReader("C:\Users\Geoff\Desktop\audio\Alice Cooper - Schools Out.mp3")
        MPLAYER.Init(filereader)
        MPLAYER.Play()

It plays perfect. Any idea how to change the sound card, I think its got something to do with waveout. but not sure on the actual command.

Perhaps this link may help you.
Click Here

Also, you may consider reading NAudio's document "Understanding Output Devices" Here

Basically, it would go like this:

    Dim devNum As Int32
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim i As Int32
            For i = -1 To WaveOut.DeviceCount - 1
                Dim woc As WaveOutCapabilities = WaveOut.GetCapabilities(i)
                ComboBox1.Items.Add(woc.ProductName)
            Next
            ComboBox1.SelectedIndex = 0
        Catch ex As Exception
        End Try
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        devNum = ComboBox1.SelectedIndex - 1
    End Sub

Unfortunatly all in c# with no real corelation vb.

commented: The library is for .NET apps. VB.NET is .NET. However I understand that people want code for their choice of .NET language. +16

yes understand, It is difficult to describe vb.net (basic) rather than vb.net (c) I need the basic vertion.

Not to be overly pedantic, but: .NET Framework is the library and runtime support system used by a number of different Microsoft languages. Visual Basic.NET is one language, C# is another, F# is yet another. The same .NET libraries work for all of them.

There may not be examples of how to use NAudio in VB.NET in the documentation, but it should be possible to do so with minimal effort. It is simply a matter of figuring out how to use the library classes and functions from VB.NET.

Here is a sample out from the documentation translated into VB.NET.

Imports System.ComponentModel
Imports NAudio.Wave

Public Class Form1
    Dim devNum As Int32
    Dim sFile As String
    Dim reader As AudioFileReader
    Dim outDev As WaveOut
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim i As Int32
            For i = -1 To WaveOut.DeviceCount - 1
                Dim woc As WaveOutCapabilities = WaveOut.GetCapabilities(i)
                ComboBox1.Items.Add(woc.ProductName)
            Next
            ComboBox1.SelectedIndex = 0
            With OpenFileDialog1
                .Filter = "All Media Files|*.wav;*.aac;*.wma;*.wmv;*.avi;*.mpg;*.mpeg;*.m1v;*.mp2;*.mp3;*.mpa;*.mpe;*.m3u;*.mp4;*.mov;*.3g2;*.3gp2;*.3gp;*.3gpp;*.m4a;*.cda;*.aif;*.aifc;*.aiff;*.mid;*.midi;*.rmi;*.mkv;*.WAV;*.AAC;*.WMA;*.WMV;*.AVI;*.MPG;*.MPEG;*.M1V;*.MP2;*.MP3;*.MPA;*.MPE;*.M3U;*.MP4;*.MOV;*.3G2;*.3GP2;*.3GP;*.3GPP;*.M4A;*.CDA;*.AIF;*.AIFC;*.AIFF;*.MID;*.MIDI;*.RMI;*.MKV"
            End With
        Catch ex As Exception
        End Try
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        devNum = ComboBox1.SelectedIndex - 1
    End Sub

    Private Sub Btn_File(sender As Object, e As EventArgs) Handles btnFile.Click
        OpenFileDialog1.ShowDialog()
    End Sub
    Private Sub OpenFile() Handles OpenFileDialog1.FileOk
        Try
            CloseAudio() ' Close if it's not first audio to play '
            sFile = OpenFileDialog1.FileName
            Dim vNom() As String = Split(sFile, "\")
            ' Get Audio File Name: '
            lblFile.Text = "File: " + vNom(vNom.Length - 1).Split(".")(0)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub BtnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
        Try
            If outDev Is Nothing Then
                outDev = New WaveOut()
            End If
            If reader Is Nothing Then
                reader = New AudioFileReader(sFile)
                outDev.Init(reader)
            End If
            outDev.Play()
        Catch ex As Exception
        End Try
    End Sub
    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        outDev.Stop()
    End Sub
    Private Sub CloseAudio()
        Try
            If outDev IsNot Nothing Then
                outDev.Stop()
                outDev = Nothing
            End If
            If reader IsNot Nothing Then
                reader.Dispose()
                reader = Nothing
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        CloseAudio()
    End Sub

    Private Sub BtnRewind_Click(sender As Object, e As EventArgs) Handles btnRewind.Click
        reader.Position = 0
    End Sub
End Class

NAudio.PNG

commented: Most excellent example. +1 +16

Thanks for the code, does this mean devnum is the sound card that I want to use after selecting the combo box?

So how do I make it play the sound from that sound card.

By the way this is all from code, so I wont be using combo boxes etc.

Currently I have MPLAYER.Play()
Which I need it to come out of a different sound card.
I also want to be able to play two different sounds out of two different card at the same time

Yes, devnum is the sound card you will be using, but if I were you, I would stick to the sample. After Googling this is what it is said about employing two sound cards at a time:

«Can you run 2 sound cards at the same time?
In most cases you'll be able to run several different soundcards side by side without problems, although there are no guarantees, and some rare combinations may suffer from audio clicks and pops, or cause your computer to crash occasionally or even refuse to boot up at all.»

Yes thank you for this code. However, changing the sound card does not change the sound card. It still plays through the same one. This is the problem I am having.
I can;t work out where the actual changing of the card code is, I can see it is the variable devNum but I cant see where devnum actually changes the output. Which in fact it doesn't.

what do you mean by stick to the sample, as the sample does not work as it does not change the sound card?

Sorry about that. I forgot the device number:

    Private Sub BtnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
        Try
            If outDev Is Nothing Then
                outDev = New WaveOut()
                outDev.DeviceNumber = devNum ' Here you set the Device Number '
            End If
            If reader Is Nothing Then
                reader = New AudioFileReader(sFile)
                outDev.Init(reader)
            End If
            outDev.Play()
        Catch ex As Exception
        End Try
    End Sub

perfect, thats what I was looking for.
Tried it on the sample code and it works for first play, but you cant change it, but I'll work on that now I have the code for devicenumber, perfect thanks.

You are welcome. I can change the audio file, click the Play button again and it works for me.
Also, I found that BtnStop_Click() should test for Nothing:

    Private Sub BtnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        If outDev IsNot Nothing Then
            outDev.Stop()
        End If
    End Sub

OK, After all that I finally (with the help of https://www.daniweb.com/members/815196/xrjf) worked it out so here is the simplified code to play two different sounds out of two different cards at the same time.

Imports NAudio
Imports NAudio.Wave
Public Class Form1

    Private mplayer1 As WaveOut
    Private mplayer2 As WaveOut
    Private filereader As AudioFileReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        filereader = New AudioFileReader("C:\first_track.mp3")
        mplayer1 = New WaveOut()
        mplayer1.DeviceNumber = 0
        mplayer1.Init(filereader)
        mplayer1.Play()

        filereader = New AudioFileReader("C:\second_track.mp3")
        mplayer2 = New WaveOut()
        mplayer2.DeviceNumber = 1
        mplayer2.Init(filereader)
        mplayer2.Play()

    End Sub
End Class

Remember to dispose the audio objects, otherwise there could be leaks of memory.
Also, could you mark as solved if you think so? Many thanks.

yep, how do I mark it as solved?

If I use the code mplayer1.Dispose()
this will end the current playing audio. Is that correct. So if I reuse the code mplayer1 = New WaveOut() does that mean I have effectily dissposed of the first audio?
So could the code go

mplayer1.Dispose()
mplayer1 = New WaveOut()

to end the first sound and start the new one. Would that stop any leakage.

Another interesting thing to watch out for is the default device needs to use devicenumber 0 rather than is actual device number, otherwise it does not play. Just a windows odity I guess.

The default device is DeviceNumber = -1.

ah of course it is that explains it perfectly.

Thanks for everyones help on this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.