C#设置wav音量
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MediaApp
{
public partial class Form1 : Form
{
[DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
public static extern int waveOutSetVolume(int uDeviceID, int dwVolume);
public Form1()
{
InitializeComponent();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
System.UInt32 Value = (System.UInt32)((double)0xffff * (double)trackBar1.Value / (double)(trackBar1.Maximum - trackBar1.Minimum));//先把trackbar的value值映射到0x0000~0xFFFF范围
//限制value的取值范围
if (Value < 0) Value = 0;
if (Value > 0xffff) Value = 0xffff;
System.UInt32 left = (System.UInt32)Value;//左声道音量
System.UInt32 right = (System.UInt32)Value;//右
waveOutSetVolume(0, (Int32)(left < < 16 | right)); //" < <"左移,“|”逻辑或运算
}
}
}
问题是,这样设置了音量后,虽然实际上已经生效了,但还不能在系统音量面板中马上看到效果反映,要重新打开面板才能看到。
另外:int waveOutSetVolume(int uDeviceID, int dwVolume);中dwVolumn直接声明为UINT来使用,也是可以的。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MediaApp
{
public partial class Form1 : Form
{
[DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
public static extern int waveOutSetVolume(int uDeviceID, int dwVolume);
public Form1()
{
InitializeComponent();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
System.UInt32 Value = (System.UInt32)((double)0xffff * (double)trackBar1.Value / (double)(trackBar1.Maximum - trackBar1.Minimum));//先把trackbar的value值映射到0x0000~0xFFFF范围
//限制value的取值范围
if (Value < 0) Value = 0;
if (Value > 0xffff) Value = 0xffff;
System.UInt32 left = (System.UInt32)Value;//左声道音量
System.UInt32 right = (System.UInt32)Value;//右
waveOutSetVolume(0, (Int32)(left < < 16 | right)); //" < <"左移,“|”逻辑或运算
}
}
}
问题是,这样设置了音量后,虽然实际上已经生效了,但还不能在系统音量面板中马上看到效果反映,要重新打开面板才能看到。
另外:int waveOutSetVolume(int uDeviceID, int dwVolume);中dwVolumn直接声明为UINT来使用,也是可以的。