参考网址
在unity中很多的音量控制dll都没有办法使用,调用"user.dll"话只是模拟电脑的快捷键,而且不能够直接设置对应的数值.
需要使用C++创建Dll 生成C++ dll的教程
打包好的dll
#include "stdafx.h"
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#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(¤tVolume);
}
else if (vUnit == VolumeUnit::Scalar) {
//Current volume as a scalar
hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
}
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 static class VolumeCtrl
{
#region 引用C++音量控制库
//获取音量的时候所使用的单位
public enum VolumeUnit
{
//分贝
Decibel,
//大小
Scalar
}
/// <summary>
/// 获取当前音量
/// </summary>
/// <param name="vUnit">当前音量的单位</param>
[DllImport("ChangeVolumeWindows")]
public static extern float GetSystemVolume(VolumeUnit vUnit);
/// <summary>
///设置当前音量
/// </summary>
/// <param name="newVolume">设置的音量小</param>
/// <param name="vUnit">音量的单位</param>
[DllImport("ChangeVolumeWindows")]
public static extern void SetSystemVolume(double newVolume, VolumeUnit vUnit);
#endregion
/// <summary>
/// 设置音量
/// </summary>
/// <param name="value">设置的音量大小</param>
/// <returns></returns>
public static float SetVolume(int value) {
float valueVolume = value / 100;
SetSystemVolume(valueVolume, VolumeUnit.Scalar);
return valueVolume;
}
/// <summary>
/// 提升或下降音量
/// </summary>
/// <param name="num">音量下降的梯度大小</param>
/// <returns></returns>
public static float ChangeVolume(int num = 2)
{
float nowVolume = GetSystemVolume(VolumeUnit.Scalar);
nowVolume += (float)num/100;
SetSystemVolume(nowVolume, VolumeUnit.Scalar);
return nowVolume;
}
static double _saveMuteVolume = 0;
/// <summary>
/// 设置静音
/// </summary>
/// <returns></returns>
public static bool MuteVolume()
{
float nowVolume = GetSystemVolume(VolumeUnit.Scalar);
if (nowVolume > 0)
{
_saveMuteVolume = nowVolume;
Debug.Log(_saveMuteVolume);
SetSystemVolume(0, VolumeUnit.Scalar);
return true;
}
else
{
SetSystemVolume(_saveMuteVolume, VolumeUnit.Scalar);
return false;
}
}
}
|