Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console app - Issue to get the list of devices #1204

Open
AlbertoVPersonal opened this issue Jan 29, 2025 · 5 comments
Open

Console app - Issue to get the list of devices #1204

AlbertoVPersonal opened this issue Jan 29, 2025 · 5 comments

Comments

@AlbertoVPersonal
Copy link

Hi all,

Description

I would like to get the list of devices but there is a class that does not exist and I don't know why.

Enviroment

  • app type: console
  • .NET version: 8.0
  • NAudio library version: 2.2.1

Steps to reproduce

Create a sample console app with the code (see below).

Current issue

WaveOut does not exist in the current context.

Expected behaviour

Not applicable

Other alternatives

Using the WASAPI enumerator, I get the list of devices. But the device number is not the same to the WaveOutEvent class.

Source code sample

using NAudio.Wave;

namespace ConsoleAppAudioDevices
{
    internal class Program
    {
        static void Main(string[] args)
        {
            const string AUDIO_PATH = "sounds/my_sound.wav";

            #region USE WAVE OUT

            for (int n = -1; n < WaveOut.DeviceCount; n++)
            {
                var caps = WaveOut.GetCapabilities(n);
                Console.WriteLine($"{n}: {caps.ProductName}");

                using (var audioFile = new AudioFileReader(AUDIO_PATH))
                using (var outputDevice = new WaveOutEvent())
                {
                    outputDevice.DeviceNumber = n;
                    outputDevice.Init(audioFile);
                    outputDevice.Play();
                    while (outputDevice.PlaybackState == PlaybackState.Playing)
                    {
                        Thread.Sleep(100);
                    }
                }

                Thread.Sleep(500);
            }

            #endregion

        }
    }
}

Thanks for advance,
Alberto

@rdyhalt
Copy link

rdyhalt commented Jan 30, 2025

@AlbertoVPersonal use WASAPI. It is the "new" way for audio playpack and recording.
WaveIn/WaveOut is the old way in Windows.

@AlbertoVPersonal
Copy link
Author

@rdyhalt I tested that version but the loopback did not run well. Possibly I had some wrong code.
Do you have a sample or a link to check?

Thanks for advance!

@rdyhalt
Copy link

rdyhalt commented Jan 31, 2025

You app will start playback. But you use Thread.Sleep on the main thread. That does not work as expected.

Try Google some code examples for NAudio.

@JustArion
Copy link
Contributor

JustArion commented Feb 8, 2025

@AlbertoVPersonal

namespace ConsoleAppAudioDevices
{
    using NAudio.CoreAudioApi;
    using NAudio.Wave;

    internal class Program
    {
        internal static async Task Main(string[] args)
        {
            const string AUDIO_PATH = "sounds/my_sound.wav";

            using var enumerator = new MMDeviceEnumerator();
            var defaultDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

            var devices = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)
                .Where(x => x.ID != defaultDevice.ID).Prepend(defaultDevice);
            
            foreach (var device in devices)
            {
                using var player = new WasapiOut(device, AudioClientShareMode.Shared, false, 0);

                var tcs = new TaskCompletionSource();
                await using var audioFile = new AudioFileReader(AUDIO_PATH);
                player.Init(audioFile);
                player.Play();

                player.PlaybackStopped += (_, _) => tcs.SetResult();

                await tcs.Task;
            }
        }
    }
}

Also for your initial problem, you can change

<TargetFramework>net8.0</TargetFramework>

to

<TargetFramework>net8.0-windows</TargetFramework>

@AlbertoVPersonal
Copy link
Author

AlbertoVPersonal commented Feb 28, 2025

Hi @JustArion,

I have had to update the code to the following (see below).
If I don't include the loop, the code is not played continuously.
My sound has a duration of one second. Maybe it is the cause.

using var enumerator2 = new MMDeviceEnumerator();
var defaultDevice = enumerator2.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

var devices2 = enumerator2.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)
    .Where(x => x.ID != defaultDevice.ID).Prepend(defaultDevice);

int i = 0;
var tcs = new TaskCompletionSource<int>(); // defined a return type
foreach (var device in devices2)
{
    while (i <= 5)
    {
        // your code
        // using var player = new WasapiOut(device, AudioClientShareMode.Shared, false, 0);

       // my version
        using var player = new WasapiOut(device, AudioClientShareMode.Shared, true, 200);
        await using var audioFile = new AudioFileReader(AUDIO_PATH);
        player.Init(audioFile);
        player.Play();
        while (player.PlaybackState == PlaybackState.Playing) ; // added
        //player.PlaybackStopped += (_, _) => tcs.SetResult(25);

        ++i;
    }
    if (!tcs.Task.IsCompleted)
    {
        tcs.SetResult(25);
    }                
}

return await tcs.Task;

Thanks for advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants