How can I notify my application that the default sound playback device has changed?

岁酱吖の 提交于 2019-12-13 00:54:53

问题


I have two sound cards on my Win XP SP3 computer, and I've written a C++ app, with which I change the default playback device by editing the following Registry entry:

regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Multimedia\Sound Mapper", true);

My app changes the "Playback" key value for the purposes of using the first or second sound card as the default playback device.

The problem is when I change the default device, the application still uses the old one (which was set as the default when the program starts). If after change, I launch the application again, everything works fine and I can use the "new" default playback device.

How can I "tell" for my application that I have changed the default device? By what way does the application read and store the variable on starting up which sound device is default in Windows? Is there any solution for my problem?


回答1:


The Registry is essentially a database that stores the default settings. Modifying the values in the registry does not cause any application, nor Windows itself, to re-initialize its settings with the new, updated values. Raymond Chen discusses this very thing with reference to user interface settings.

Also consider that things like this are very likely to change in later versions of Windows. If you ever decide to update to Windows Vista or 7, you'll be back here again asking more questions because your sound-switcher application won't work anymore. The later versions handle audio devices very differently than they were handled in XP; for starters, they're now based around the Core Audio APIs.

Therefore, for reasons that should be obvious, modifying registry values is not the preferred way to modify your computer's configuration.

But if you're just trying to make a particular application notice that you've changed the value in the registry, the simple solution is the RegNotifyChangeKeyValue function. This essentially subscribes the application to receive notifications each time the value of a particular registry key changes.

The correct solution for Windows Vista and later is available here.



来源:https://stackoverflow.com/questions/5675786/how-can-i-notify-my-application-that-the-default-sound-playback-device-has-chang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!