16005

Get default output audio device with NAudio

I want to get the default output audio device (i.e. my speakers) using NAudio, to get the master sound volume as in this question.

I am trying to use MMDeviceEnumerator.GetDevice(), but the id it takes is a string, not the device number. Here's the code I've written so far:

var enumerator = new MMDeviceEnumerator(); for (int i = 0; i < WaveOut.DeviceCount; i++) { var cap = WaveOut.GetCapabilities(i); Console.WriteLine("{0}: {1}", i, cap.ProductName); var device = enumerator.GetDevice(???); } Console.WriteLine(); Console.ReadLine();

I've tried passing the various Guids from the capabilities, as well as the device id in string format, to GetDevice() but none of them work.

How do I get the default device?

Answer1:

You are mixing two completely different audio APIs here. MMDeviceEnumerator is part of WASAPI, the new audio API introduced in WindowsVista, and WaveOut.DeviceCount uses the old Windows audio APIs.

To use WASAPI to get the default audio device, you use code like this:

var enumerator = new MMDeviceEnumerator(); enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);

There are actually three different types of default audio output device, depending on the purpose (role):

/// <summary> /// Games, system notification sounds, and voice commands. /// </summary> Console, /// <summary> /// Music, movies, narration, and live music recording /// </summary> Multimedia, /// <summary> /// Voice communications (talking to another person). /// </summary> Communications,

Recommend

  • Playing part of a sound (WMA) file in C#
  • Play sound in both speaker and headset wpf
  • How to read dynamic returned JSON string in c#?
  • WEKA - Error on assigning Instances to kmeans.buildClusterer
  • Collection was modified; enumeration operation may not execute [duplicate]
  • Completely enumerate indices of D-dimensional array at compile time
  • How to write nested JSON using Jackson?
  • Memory Requirement/Utilization for MongoDB, Riak and HyperTable (or HBase)
  • How to skip objects while iterating in a ruby like Map method for obj-c
  • Sympy - fraction manipulation
  • Rails rubber - cap command not found
  • Can't open HTTP stream: Error opening file (../cap_ffmpeg_impl.hpp:529)
  • Matplotlib save file as JPEG encoder error
  • How do I bind multiple properties in an Android layout element
  • Sending cookie value via httpget but not getting the desired response
  • hide missing dates from x-axis ggplot2
  • Find Previous month name using Calender or any classes that in java
  • How integrated is Collada to OpenGL ES
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Textfile Structure (tables)
  • Email format validation in mvc3 view
  • Cannot connect to cassandra from Spark
  • Fill an image in a square container while keeping aspect ratio
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • How to pass list parameters for each object using Spring MVC?
  • Proper way to use connect-multiparty with express.js?
  • using HTMLImports.whenReady not working in chrome
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Authorize attributes not working in MVC 4
  • Is it possible to post an object from jquery to bottle.py?
  • EntityFramework adding new object to nested object collection
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal