0

I have a program where the user can select the output device and play a sound. I am using NAudio, because it was the easiest way to have a way to select the output device. However it seems too hard for me to find a way to loop the sound in NAudio. Here's my code:

    Dim xa() As Byte = IO.File.ReadAllBytes("/path") 'Your Buffer
    Dim Wave1 As New NAudio.Wave.WaveOut 'Wave out device for playing the sound
    Dim data As New IO.MemoryStream(xa) 'Data stream for the buffer

    Dim deviceEnum = New MMDeviceEnumerator()
    Dim devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active).ToList()
    Dim i = 0
    Dim outputIndex = 0
    For Each d As MMDevice In devices 'select output device index
        If d.ToString = MediaOutputDevice Then
            outputIndex = i
        End If
        i = i + 1
    Next
    Wave1.DeviceNumber = outputIndex
    Wave1.Init(New NAudio.Wave.WaveFileReader(data))
    Wave1.Play()

Any help for the sound alert loop in Naudio? I know I could play the sound with

Sub PlayLoopingBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav",
        AudioPlayMode.BackgroundLoop)
End Sub

but then I can't choose the output device.

Mirba
  • 1
  • Does this answer your question? [NAudio looping an audio file](https://stackoverflow.com/questions/8792456/naudio-looping-an-audio-file) – Andrew Morton Sep 10 '21 at 14:12
  • Theres only an example with C#, thanks anyways maybe if there's no more answers I should try to convert that code to vb – Mirba Sep 10 '21 at 14:15
  • 3
    It is useful to be able to convert from C# to VB.NET yourself as a lot of examples are only available in C#. There are code convertors available which will usually do most of the work for you, e.g., the [one from Telerik](https://converter.telerik.com/). – Andrew Morton Sep 10 '21 at 14:18
  • 4
    You can add a C# Project to a Solution that has a main (starting) VB.Net Project. You can then reference that C# Project from your VB.Net code. I.e., you can build a Class Library that uses C# code and *export* methods that can be used by any other Project in a Solution. There's nothing special you need to do, it's the same as having a VB.Net Class Library Project, just the code is C#. – Jimi Sep 10 '21 at 14:28

0 Answers0