Choosing the default playback device in Xp
The theme of this post is one I have been fascinated for the last few days. The situation arises when you have more than one audio device attached to your computer. One might be the default audio device for your system while the other might be a USB audio device( like another audio usb board or a usb headset ). In the application I am working on, the user is required to record an audio note through an iMic usb system and in order to listen to that note, the default system playback device must be used. ( I will not waste the time explaining why, it is not relevant ). This configuration applies in other circumstances as well and I thought it would be usefull to post an entry about it.
As you are probably aware, when inserting a new usb audio device into your computer’s usb port, the default playback and recording device is set to the new usb device. You can do something about that too, but only if you write a custom driver for the usb devices such that they are not set as defaults upon insertion.
There is no clean way to change the playback and recording devices programatically. And there seems to be NO way in Vista. You must set the default playback and recording devices manually from Control Panel/ Sounds and Audio Devices. And this is a “feature” of the operating system, not a bug.
Oh, well… I’ve found a solution for this problem in XP. The basic idea is that you must modify a registry key and then restart explorer.exe. My implementation is in C#, but this is not language dependent.
The Registry :
I assume you are familiar with the Audio setting control panel, and I will jump to presenting the registry entries you must change.
First, you must open the registry editor and browse to the following key :
HKEY_CURRENT_USER\Software\Microsoft\Multimedia\Sound Mapper\
This is how the registry keys under Sound Mapper look like in my case. As you can see, the UserPlayback and UserRecord keys are filled with the system’s default audio device. The Playback and Record keys represent the current playback and recording devices.
Implementation :
Bellow, I give the two methods I use to get and set the audio playback devices:
public string GetDefaultDevice( )
{
RegistryKey soundMapper = null;
soundMapper = Registry.CurrentUser.OpenSubKey(“Software”).OpenSubKey(“Microsoft” ).OpenSubKey( “Multimedia” ).OpenSubKey( “Sound Mapper”, true );
if ( soundMapper != null )
{
string userPlayback= Convert.ToString(soundMapper.GetValue(“UserPlayback”, string.Empty));
return userPlayback;
}
return string.Empty;
}
/// <summary>
/// Sets the audio device with the provided description as the default playback device.
/// USE WITH CARE. THE DEVICE YOU MAY BE TRYING TO SET AS DEFAULT
///MAY NOT EXIST IN YOUR HARDAWARE CONFIGURATION
///</summary>
/// <param name=”deviceDescription”>Audio device description to be written to registry.</param>
public void SetDefaultDevice( string deviceDescription )
{
RegistryKey soundMapper = null;
soundMapper = Registry.CurrentUser.OpenSubKey(“Software”).OpenSubKey(“Microsoft” ).OpenSubKey( “Multimedia” ).OpenSubKey( “Sound Mapper”, true );
if ( soundMapper != null )
{
string playbackDev = Convert.ToString( soundMapper.GetValue( “Playback”, string.Empty ) );
if ( playbackDev != string.Empty )
{
if ( playbackDev != deviceDescription )
{
soundMapper.SetValue( “Playback”, deviceDescription, RegistryValueKind.String );
soundMapper.Flush();
soundMapper.Close( );
// restart explorer.exe :
// 1. kill explorer
foreach(Process p in Process.GetProcessesByName(“explorer”))
{
try
{p.Kill( );
}
catch ( Exception ex )
{
System.Diagnostics.Debug.WriteLine( ex.Message + “\n” + ex.StackTrace );
}
}
// 2. the OS restarts explorer automatically
return;
}
}
soundMapper.Close( );
}
}
Note that these two can be used together, when you want to set the playback device as the default device :
string defaultDevice = GetDefaultDevice( );
SetDefaultDevice( defaultDevice );
However, you can use the SetDefaultDevice method to set the Playback device to any device you want, as long as it exists. You must know the device description string in order to do that. This string is the one presented in the Audio settings control panel and should be an exact match in your code.
You can list the audio devices yourself, from inside your code, if you are willing to use the DirectX library.
Note that in SetDefaultDevice, I have killed all explorer processes in the system. Windows will restart explorer automatically, so you don’t have to worry about that.
If you are using forms, do not make the registry chages inside the Load event handler but in the constructor. Otherwise, the changes will not be seen until the next time you restart your application.
So, I hope this will be of any help to you. And if you find another solution to this issue, I will be glad to hear about it. Thanks.
-
Archives
- May 2009 (1)
- April 2009 (4)
- February 2008 (1)
- November 2007 (1)
- August 2007 (1)
- July 2007 (2)
- June 2007 (2)
- May 2007 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS
