私はまったく同じ問題に遭遇しました。幸いにも私は、P/Invokeを使用してC#インターフェイスからIAudioEndpointVolumeを使用することで、実用的なソリューションを見つけました。
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Windows.Media.Devices;
namespace AudioUtils
{
public static class VolumeControl
{
public static void ChangeVolumeToMinLevel(double level)
{
if (level > 1)
level = 1;
else if (level < 0)
level = 0;
try
{
var masterVol = GetAudioEndpointVolume();
if (masterVol == null)
return;
var hr = masterVol.GetMute(out var muted);
if (hr != (uint)HResult.S_OK)
return;
if (muted)
{
masterVol.SetMute(false, Guid.Empty);
}
// Only adapt volume if the current level is below the specified minumum level
hr = masterVol.GetMasterVolumeLevelScalar(out float currentAudioValue);
float newAudioValue = Convert.ToSingle(level);
if (currentAudioValue > newAudioValue)
return;
masterVol.SetMasterVolumeLevelScalar(newAudioValue, Guid.Empty);
}
catch { }
}
private static IAudioEndpointVolume GetAudioEndpointVolume()
{
var speakerId = MediaDevice.GetDefaultAudioRenderId(AudioDeviceRole.Default);
var completionHandler = new ActivateAudioInterfaceCompletionHandler<IAudioEndpointVolume>();
var hr = ActivateAudioInterfaceAsync(
speakerId,
typeof(IAudioEndpointVolume).GetTypeInfo().GUID,
IntPtr.Zero,
completionHandler,
out var activateOperation);
Debug.Assert(hr == (uint)HResult.S_OK);
return completionHandler.WaitForCompletion();
}
[DllImport("Mmdevapi.dll", ExactSpelling = true, PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Error)]
private static extern uint ActivateAudioInterfaceAsync(
[In, MarshalAs(UnmanagedType.LPWStr)]string deviceInterfacePath,
[In, MarshalAs(UnmanagedType.LPStruct)]Guid riid,
[In] IntPtr activationParams,
[In] IActivateAudioInterfaceCompletionHandler completionHandler,
out IActivateAudioInterfaceAsyncOperation activationOperation);
}
internal class ActivateAudioInterfaceCompletionHandler<T> : IActivateAudioInterfaceCompletionHandler
{
private AutoResetEvent _completionEvent;
private T _result;
public ActivateAudioInterfaceCompletionHandler()
{
_completionEvent = new AutoResetEvent(false);
}
public void ActivateCompleted(IActivateAudioInterfaceAsyncOperation operation)
{
operation.GetActivateResult(out var hr, out var activatedInterface);
Debug.Assert(hr == (uint)HResult.S_OK);
_result = (T)activatedInterface;
var setResult = _completionEvent.Set();
Debug.Assert(setResult != false);
}
public T WaitForCompletion()
{
var waitResult = _completionEvent.WaitOne();
Debug.Assert(waitResult != false);
return _result;
}
}
internal enum HResult : uint
{
S_OK = 0
}
[ComImport]
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IAudioEndpointVolume
{
[PreserveSig]
int NotImpl1();
[PreserveSig]
int NotImpl2();
[PreserveSig]
int GetChannelCount([Out] [MarshalAs(UnmanagedType.U4)] out uint channelCount);
[PreserveSig]
int SetMasterVolumeLevel(
[In] [MarshalAs(UnmanagedType.R4)] float level,
[In] [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int SetMasterVolumeLevelScalar(
[In] [MarshalAs(UnmanagedType.R4)] float level,
[In] [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int GetMasterVolumeLevel([Out] [MarshalAs(UnmanagedType.R4)] out float level);
[PreserveSig]
int GetMasterVolumeLevelScalar([Out] [MarshalAs(UnmanagedType.R4)] out float level);
[PreserveSig]
int SetChannelVolumeLevel(
[In] [MarshalAs(UnmanagedType.U4)] UInt32 channelNumber,
[In] [MarshalAs(UnmanagedType.R4)] float level,
[In] [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int SetChannelVolumeLevelScalar(
[In] [MarshalAs(UnmanagedType.U4)] uint channelNumber,
[In] [MarshalAs(UnmanagedType.R4)] float level,
[In] [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
int GetChannelVolumeLevel(
[In] [MarshalAs(UnmanagedType.U4)] uint channelNumber,
[Out] [MarshalAs(UnmanagedType.R4)] out float level);
[PreserveSig]
int GetChannelVolumeLevelScalar(
[In] [MarshalAs(UnmanagedType.U4)] uint channelNumber,
[Out] [MarshalAs(UnmanagedType.R4)] out float level);
[PreserveSig]
int SetMute(
[In] [MarshalAs(UnmanagedType.Bool)] bool isMuted,
[In] [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int GetMute([Out] [MarshalAs(UnmanagedType.Bool)] out bool isMuted);
[PreserveSig]
int GetVolumeStepInfo(
[Out] [MarshalAs(UnmanagedType.U4)] out uint step,
[Out] [MarshalAs(UnmanagedType.U4)] out uint stepCount);
[PreserveSig]
int VolumeStepUp([In] [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int VolumeStepDown([In] [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
[PreserveSig]
int QueryHardwareSupport([Out] [MarshalAs(UnmanagedType.U4)] out uint hardwareSupportMask);
[PreserveSig]
int GetVolumeRange(
[Out] [MarshalAs(UnmanagedType.R4)] out float volumeMin,
[Out] [MarshalAs(UnmanagedType.R4)] out float volumeMax,
[Out] [MarshalAs(UnmanagedType.R4)] out float volumeStep);
}
[ComImport]
[Guid("72A22D78-CDE4-431D-B8CC-843A71199B6D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IActivateAudioInterfaceAsyncOperation
{
void GetActivateResult(
[MarshalAs(UnmanagedType.Error)]out uint activateResult,
[MarshalAs(UnmanagedType.IUnknown)]out object activatedInterface);
}
[ComImport]
[Guid("41D949AB-9862-444A-80F6-C261334DA5EB")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IActivateAudioInterfaceCompletionHandler
{
void ActivateCompleted(IActivateAudioInterfaceAsyncOperation activateOperation);
}
}
クレジット
では動作しませんでした
危険なコードを使用しています
suniusによって提供されたコードは、2つの主要な欠点を持っていました危険なコードとクラッシュのより明示的なマーシャリング属性が使用され、署名が保持される(vannatechのソースコードを参照)
あなたのアプリケーションをここのようなアラームアプリケーションとして定義する:https://msdn.microsoft.com/en-us/magazine/dn451440.aspx私はそれをテストしていないが、理論的には通常のアラーム通知がサウンドを再生していない場合は、アラーム固有のアプリを作成する必要があります。電話のAlarms&Clockアプリでアラームを設定すると、アラームの音量はシステムの音量とは無関係になるためです。欠点は、あなたのアプリがW10でどのように処理されているのかわからない、アラームのapp機能を置き換えるということです。 – schumi1331
編集:これはUWPで今扱いが異なるようです。 Hm。あなたのマニフェストに を追加しましたか? –
schumi1331
ヒントについてはThx、私はそれを試し、それが働いた場合にお知らせします。アプリのUWP版は現在保留中ですが、私は約1週間で続きます – Li0NsD0MaIn