如何以编程方式设置系统卷?

时间:2012-10-30 12:40:19

标签: c# .net audio

如何使用C#应用程序更改Windows系统音量?

6 个答案:

答案 0 :(得分:61)

我参加派对有点晚了,但如果你现在正在寻找可用的nuget包(AudioSwitcher.AudioApi.CoreAudio),它可以简化音频交互。安装它然后就像这样简单:

CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
Debug.WriteLine("Current Volume:" + defaultPlaybackDevice.Volume);
defaultPlaybackDevice.Volume = 80;

答案 1 :(得分:47)

以下是代码:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Test
{
    public class Test
    {
        private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
        private const int APPCOMMAND_VOLUME_UP = 0xA0000;
        private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
        private const int WM_APPCOMMAND = 0x319;

        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
            IntPtr wParam, IntPtr lParam);

        private void Mute()
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_MUTE);
        }

        private void VolDown()
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_DOWN);
        }

        private void VolUp()
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_UP);
        }
    }
}

dotnetcurry

上找到

使用WPF时,您需要使用new WindowInteropHelper(this).Handle代替this.Handle (感谢Alex Beals)

答案 2 :(得分:12)

如果其他答案中提供的教程过于复杂,您可以使用keybd_event function

尝试这样的实现
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

<强>用法:

keybd_event((byte)Keys.VolumeUp, 0, 0, 0); // increase volume
keybd_event((byte)Keys.VolumeDown, 0, 0, 0); // decrease volume

答案 3 :(得分:10)

如果您希望使用Core Audio API将其设置为精确值:

using CoreAudioApi;

public class SystemVolumeConfigurator
{
        private readonly MMDeviceEnumerator _deviceEnumerator = new MMDeviceEnumerator();
        private readonly MMDevice _playbackDevice;

        public SystemVolumeConfigurator()
        {
            _playbackDevice = _deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
        }

        public int GetVolume()
        {
            return (int)(_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
        }

        public void SetVolume(int volumeLevel)
        {
            if (volumeLevel < 0 || volumeLevel > 100)
                throw new ArgumentException("Volume must be between 0 and 100!");

            _playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar = volumeLevel / 100.0f;
        }
}

答案 4 :(得分:0)

我的代码有些不同,但仍使用CoreAudio

下载了pkg: nuget安装AudioSwitcher.AudioApi.CoreAudio -Version 3.0.0.1

import {createStackNavigator} from 'react-navigation';

const RootStack = createStackNavigator(
{
    Home: {
        screen: Home,
    },
    Land: {
        screen: On,
    },
    Find: {
        screen: Find
    }
    },
    {
    initialRouteName: 'Home',
    }
    );

    export default class App extends Component {
    constructor(props) {
    super(props);
    this.state = {
        timePassed: false,
        On: true,
    };
    }
    render() {
        return (
            <View style={styles.container}>
                <StatusBar backgroundColor='#F2C490' translucent={true} barStyle='light-content'/><RootStack/>
            </View>
        );

    }
}
}

答案 5 :(得分:0)

您可以将此库https://gist.github.com/sverrirs/d099b34b7f72bb4fb386添加到您的项目中,并像这样更改音量;

VideoPlayerController.AudioManager.SetMasterVolume(100);

该库还包含用于更改应用程序音量,静音,获取当前音量级别等的选项。 该名称空间称为“视频播放器控制器”,但是我在Windows Forms App中使用了它来更改系统音量,并且可以正常工作,因此“视频”部分是任意的。