改变系统音量

时间:2013-03-23 04:08:08

标签: c# wpf vb.net

我正在尝试从我的WPF应用程序控制系统音量,但没有任何变化。我用Google搜索了一些例子,发现了一个来自:PCVolumeControl

如果我尝试GetVolume(),它会给我0到100之间的值。如果我使用SetVolume(80) PC声音不会改变。再次,如果我尝试GetVolume(),它将返回80。

我不确定这里有什么问题。根据该网站,它可能不适用于64位,但我没有在那里看到明确的答案。

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

我从this链接获得了以下snippet,我在本地系统中tested,它工作正常,但我不确定,是否适合是WPF还是不。

问题标记:VB.NET

Imports System.Runtime.InteropServices
Public Class Form1
            <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
            End Function

            Const WM_APPCOMMAND As UInteger = &H319
            Const APPCOMMAND_VOLUME_UP As UInteger = &HA
            Const APPCOMMAND_VOLUME_DOWN As UInteger = &H9
            Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8

            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_UP * &H10000)
            End Sub
            Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_DOWN * &H10000)
            End Sub
            Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
                SendMessage(Me.Handle, WM_APPCOMMAND, &H200EB0, APPCOMMAND_VOLUME_MUTE * &H10000)
            End Sub
End Class

答案 1 :(得分:-1)

了解MediaElement Property。

以下是它的链接。

http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.volume.aspx

更改音量就像这样&gt;&gt;

<Slider Minimum="0" Maximum="1" ValueChanged="ValueChanged"/>

private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    media.Volume = e.NewValue;
}

希望它有所帮助。