我想改变程序的音量(而不是master体积)。我现在有以下代码:
DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
private void volumeBar_Scroll(object sender, EventArgs e)
{
// Calculate the volume that's being set
int NewVolume = ((ushort.MaxValue / 10) * volumeBar.Value);
// Set the same volume for both the left and the right channels
uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
// Set the volume
waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
}
这只适用于 Win XP,不适用于 Windows 7(可能也适用于 Vista)。我还没有找到任何脚本可以在 Win 7 上实现相同的效果,只是更改主卷(我不这样做)。
你的代码对我来说工作正常(经过一些调整)。以下是在 Windows 7 x64 上运行的非常简单的 WPF 测试应用程序的代码:
Xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Slider Minimum="0" Maximum="10" ValueChanged="ValueChanged"/>
</Grid>
</Window>
C#
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
// Calculate the volume that's being set
double newVolume = ushort.MaxValue * e.NewValue / 10.0;
uint v = ((uint) newVolume) & 0xffff;
uint vAll = v | (v << 16);
// Set the volume
int retVal = NativeMethods.WaveOutSetVolume(IntPtr.Zero, vAll);
Debug.WriteLine(retVal);
bool playRetVal = NativeMethods.PlaySound("tada.wav", IntPtr.Zero, 0x2001);
Debug.WriteLine(playRetVal);
}
}
static class NativeMethods
{
[DllImport("winmm.dll", EntryPoint = "waveOutSetVolume")]
public static extern int WaveOutSetVolume(IntPtr hwo, uint dwVolume);
[DllImport("winmm.dll", SetLastError = true)]
public static extern bool PlaySound(string pszSound, IntPtr hmod, uint fdwSound);
}
当我启动应用程序并移动滑块时,“音量混合器”中会出现一个附加音量控件,它与滑块同步从最小移动到最大。
您应该检查 waveOutSetVolume 的返回值。如果您的代码仍然无法工作,它可能会为您提供线索。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)