问题
I'm using NAudio to do a few tasks:
- Locate the "Stereo Mix" source line
- Un-mute the "Stereo Mix" source line by enabling any line controls present
- Mute all other source lines on the same input device by disabling any line controls present
The program I've written can perform task 1 okay, but tasks 2 and 3 fail.
Specifically, this block of code causes the ArgumentException to be thrown:
if( control.IsBoolean ) {
BooleanMixerControl boolControl = (BooleanMixerControl)control;
boolControl.Value = isMuted;
set = true;
if( boolControl.Value != isMuted )
throw new ArgumentException("Could not set line muted value.");
}
Here is a static class that I use to perform these tasks. It has a dependency on the current version of NAudio:
public static class RecordSourceManager {
public static Boolean GetMicrophoneMuted(String deviceName) {
Mixer mixer = GetMixer( deviceName );
if( mixer == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not exist.");
foreach(MixerLine line in mixer.Destinations) {
foreach(MixerLine sourceLine in line.Sources) {
if( sourceLine.ComponentType == MixerLineComponentType.SourceMicrophone ) {
return GetLineMuted( sourceLine );
}
}
}
throw new ArgumentException("Specified device \"" + deviceName + "\" does not contain a microphone device.");
}
public static void SetMicrophoneExclusive(String deviceName, Boolean enableMicrophoneExclusivity) {
Mixer mixer = GetMixer( deviceName );
if( mixer == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not exist.");
foreach(MixerLine line in mixer.Destinations) {
foreach(MixerLine sourceLine in line.Sources) {
if( sourceLine.ComponentType == MixerLineComponentType.SourceMicrophone ) {
SetLineMuted( sourceLine, !enableMicrophoneExclusivity );
} else {
SetLineMuted( sourceLine, enableMicrophoneExclusivity );
}
}
}
}
public static Boolean GetStereoMixMuted(String deviceName) {
Mixer mixer = GetMixer( deviceName );
if( mixer == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not exist.");
foreach(MixerLine line in mixer.Destinations) {
foreach(MixerLine sourceLine in line.Sources) {
if( IsStereoMix( sourceLine.Name ) ) {
return GetLineMuted( sourceLine );
}
}
}
throw new ArgumentException("Specified device \"" + deviceName + "\" does not contain a microphone device.");
}
public static void SetStereoMixExclusive(String deviceName, Boolean enableStereoMixExclusivity) {
Mixer mixer = GetMixer( deviceName );
if( mixer == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not exist.");
MixerLine stereoMix;
MixerLine parentLine;
GetStereoMixLine( mixer, out stereoMix, out parentLine );
if( stereoMix == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not contain a Stereo Mix line.");
foreach(MixerLine source in parentLine.Sources) {
Boolean ok;
if( IsStereoMix( source.Name ) ) {
ok = SetLineMuted( source, !enableStereoMixExclusivity );
} else {
ok = SetLineMuted( source, enableStereoMixExclusivity );
}
if( !ok ) throw new ArgumentException("Could not set line muted state.");
}
}
private static Mixer GetMixer(String deviceName) {
foreach(Mixer mixer in Mixer.Mixers) {
//wtr.WriteLine("Mixer: {0}, Mfg: {1}", mixer.Name, mixer.Manufacturer );
if( String.Equals( mixer.Name, deviceName, StringComparison.OrdinalIgnoreCase ) ) return mixer;
}
return null;
}
private static void GetStereoMixLine(Mixer device, out MixerLine stereoMix, out MixerLine parentLine) {
foreach(MixerLine line in device.Destinations) {
foreach(MixerLine source in line.Sources) {
if( IsStereoMix( source.Name ) ) {
stereoMix = source;
parentLine = line;
return;
}
}
}
stereoMix = null;
parentLine = null;
}
private static Boolean IsStereoMix(String sourceName) {
String[] names = new String[] {
"Stereo Mix",
"What U Hear",
"What \"U\" Hear",
"What-U-Hear",
"Playback Redirect",
"Wave Out",
"Wave Out Mix",
"Wave-Out Mix"
};
foreach(String name in names) {
if( String.Equals( sourceName, name, StringComparison.OrdinalIgnoreCase ) ) return true;
}
return false;
}
private static Boolean SetLineMuted(MixerLine line, Boolean isMuted) {
Boolean set = false;
foreach(MixerControl control in line.Controls) {
// Can't test if control's name == "Mute" because sometimes it's "Mic Volume" (even though it's boolean). Same goes for GetLineMuted.
if( control.IsBoolean ) {
BooleanMixerControl boolControl = (BooleanMixerControl)control;
boolControl.Value = isMuted;
set = true;
if( boolControl.Value != isMuted )
throw new ArgumentException("Could not set line muted value.");
}
}
return set;
}
private static Boolean GetLineMuted(MixerLine line) {
foreach(MixerControl control in line.Controls) {
if( control.IsBoolean ) {
BooleanMixerControl boolControl = (BooleanMixerControl)control;
return boolControl.Value;
}
}
return false;
}
}
I thought I'd take a look at NAudio's BooleanMixerControl class, and I see this:
public bool Value {
get {
base.GetControlDetails();
return (this.boolDetails.fValue == 1);
}
set {
MmException.Try(MixerInterop.mixerSetControlDetails(base.mixerHandle, ref this.mixerControlDetails, base.mixerHandleType), "mixerSetControlDetails");
}
}
Interestingly it seems the value
argument to the property setter is being ignored, the mixerSetControlDetails call therefore won't do any useful work. Is this a bug in NAudio?
回答1:
This feature is not implemented, and probably should be replaced with a NotImplementedException
. It was amongst the first code I ever wrote for NAudio back in 2002 when I was just beginning to learn about PInvoke and pinning, and if you look at the NAudio source you will see the code that uses the value is commented out, presumably because it caused some kind of memory exception when I originally tried it.
/// <summary>
/// The current value of the control
/// </summary>
public bool Value
{
get
{
GetControlDetails(); // make sure we have the latest value
return (boolDetails.fValue == 1);
}
set
{
//GetControlDetails();
//MixerInterop.MIXERCONTROLDETAILS_BOOLEAN boolDetails = (MixerInterop.MIXERCONTROLDETAILS_BOOLEAN) Marshal.PtrToStructure(mixerControlDetails.paDetails,typeof(MixerInterop.MIXERCONTROLDETAILS_BOOLEAN));
//boolDetails.fValue = (value) ? 1 : 0;
// TODO: pin the memory
MmException.Try(MixerInterop.mixerSetControlDetails(mixerHandle, ref mixerControlDetails, MixerFlags.Value | mixerHandleType), "mixerSetControlDetails");
}
}
The reason it has never been fixed is that I've never needed to use it myself, but if someone wants to contribute a fix I'd be glad to include it in NAudio.
回答2:
I was also having this issue. In addition to Mark Heaths reply, i adjusted the property Value of the BooleanMixerControl to retrieve a correct value:
/// <summary>
/// The current value of the control
/// </summary>
public bool Value
{
get
{
GetControlDetails(); // make sure we have the latest value
return (boolDetails.fValue == 1);
}
set
{
int structSize = Marshal.SizeOf(boolDetails);
mixerControlDetails.paDetails = Marshal.AllocHGlobal(structSize * nChannels);
for (int channel = 0; channel < nChannels; channel++)
{
boolDetails.fValue = value ? 1 : 0;
long pointer = mixerControlDetails.paDetails.ToInt64() + (structSize * channel);
Marshal.StructureToPtr(boolDetails, (IntPtr)pointer, false);
}
MmException.Try(MixerInterop.mixerSetControlDetails(mixerHandle, ref mixerControlDetails, MixerFlags.Value | mixerHandleType), "mixerSetControlDetails");
Marshal.FreeHGlobal(mixerControlDetails.paDetails);
}
This seems to work fine for me
来源:https://stackoverflow.com/questions/11111242/naudio-cannot-set-line-control-mute-value