如何在Windows中获取和设置系统卷

我想在键盘点击时使用单位和c#将操作系统音量设置在一定水平上,例如我想将Windows音量(不是统一)设置为70:我该怎么做?

void Update() { if (Input.GetKeyDown(KeyCode.A)) { //Set Windows Volume 70% } } 

这需要一个插件。 由于这个问题适用于Windows,因此您可以使用IAudioEndpointVolume构建C ++插件,然后从C#调用它。 这篇文章有一个工作的C ++示例,说明如何使用IAudioEndpointVolume更改卷,您可以使用它作为基本源来创建C ++插件。


我已经继续清理该代码然后转换为dll插件广告将DLL放在Assets / Plugins文件夹中。 您可以在此处了解如何构建C ++插件。

C ++代码

 #include "stdafx.h" #include  #include  #include  #define DLLExport __declspec(dllexport) extern "C" { enum class VolumeUnit { Decibel, Scalar }; //Gets volume DLLExport float GetSystemVolume(VolumeUnit vUnit) { HRESULT hr; // ------------------------- CoInitialize(NULL); IMMDeviceEnumerator *deviceEnumerator = NULL; hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator); IMMDevice *defaultDevice = NULL; hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice); deviceEnumerator->Release(); deviceEnumerator = NULL; IAudioEndpointVolume *endpointVolume = NULL; hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume); defaultDevice->Release(); defaultDevice = NULL; float currentVolume = 0; if (vUnit == VolumeUnit::Decibel) { //Current volume in dB hr = endpointVolume->GetMasterVolumeLevel(&currentVolume); } else if (vUnit == VolumeUnit::Scalar) { //Current volume as a scalar hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume); } endpointVolume->Release(); CoUninitialize(); return currentVolume; } //Sets volume DLLExport void SetSystemVolume(double newVolume, VolumeUnit vUnit) { HRESULT hr; // ------------------------- CoInitialize(NULL); IMMDeviceEnumerator *deviceEnumerator = NULL; hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator); IMMDevice *defaultDevice = NULL; hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice); deviceEnumerator->Release(); deviceEnumerator = NULL; IAudioEndpointVolume *endpointVolume = NULL; hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume); defaultDevice->Release(); defaultDevice = NULL; if (vUnit == VolumeUnit::Decibel) hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL); else if (vUnit == VolumeUnit::Scalar) hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL); endpointVolume->Release(); CoUninitialize(); } } 

C#代码

 using System.Runtime.InteropServices; using UnityEngine; public class VolumeManager : MonoBehaviour { //The Unity to use when getting and setting the volume public enum VolumeUnit { //Perform volume action in decibels Decibel, //Perform volume action in scalar Scalar } ///  /// Gets the current volume ///  /// The unity to report the current volume in [DllImport("ChangeVolumeWindows")] public static extern float GetSystemVolume(VolumeUnit vUnit); ///  /// sets the current volume ///  /// The new volume to set /// The unity to set the current volume in [DllImport("ChangeVolumeWindows")] public static extern void SetSystemVolume(double newVolume, VolumeUnit vUnit); // Use this for initialization void Start() { //Get volume in Decibel float volumeDecibel = GetSystemVolume(VolumeUnit.Decibel); Debug.Log("Volume in Decibel: " + volumeDecibel); //Get volume in Scalar float volumeScalar = GetSystemVolume(VolumeUnit.Scalar); Debug.Log("Volume in Scalar: " + volumeScalar); //Set volume in Decibel SetSystemVolume(-16f, VolumeUnit.Decibel); //Set volume in Scalar SetSystemVolume(0.70f, VolumeUnit.Scalar); } } 

GetSystemVolume函数用于获取当前卷, SetSystemVolume用于设置它。 这个问题适用于Windows,但您可以使用其他平台的API执行类似的操作。

按下A键时将其设置为70%

 void Update() { if (Input.GetKeyDown(KeyCode.A)) { //Set Windows Volume 70% SetSystemVolume(0.70f, VolumeUnit.Scalar); } } 

你不能从我所知道的那样做。 没有任何有益的收获,你在任何游戏/应用程序中看到过这种情况吗?

您可以使用滑块更改Unity应用程序的主音量,并以相同的方式管理其他声音效果

http://docs.unity3d.com/ScriptReference/AudioListener.html
http://docs.unity3d.com/ScriptReference/AudioListener-volume.html

 public class AudioControl: MonoBehaviour { void SetVolume(Slider slider) { AudioListener.volume = slider.Value; // probably save this value to a playerpref so it is remembered. } }