Advertisement
Guest User

Untitled

a guest
May 26th, 2016
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.07 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using Windows.ApplicationModel.Core;
  7. using Windows.Media.Devices;
  8. using Windows.UI.Core;
  9.  
  10. enum HResult
  11. {
  12.     S_OK = 0
  13. }
  14.  
  15. [ComImport]
  16. [Guid("72A22D78-CDE4-431D-B8CC-843A71199B6D")]
  17. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  18. interface IActivateAudioInterfaceAsyncOperation
  19. {
  20.     void GetActivateResult([MarshalAs(UnmanagedType.Error)]out HResult activateResult, [MarshalAs(UnmanagedType.IUnknown)]out object activatedInterface);
  21. }
  22.  
  23. [ComImport]
  24. [Guid("41D949AB-9862-444A-80F6-C261334DA5EB")]
  25. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  26. interface IActivateAudioInterfaceCompletionHandler
  27. {
  28.     void ActivateCompleted(IActivateAudioInterfaceAsyncOperation activateOperation);
  29. }
  30.  
  31. class ActivateAudioInterfaceCompletionHandler<T> : IActivateAudioInterfaceCompletionHandler
  32. {
  33.     public ActivateAudioInterfaceCompletionHandler()
  34.     {
  35.         m_CompletionEvent = new AutoResetEvent(false);
  36.     }
  37.  
  38.     public void ActivateCompleted(IActivateAudioInterfaceAsyncOperation operation)
  39.     {
  40.         HResult operationHR;
  41.         object activatedInterface;
  42.         operation.GetActivateResult(out operationHR, out activatedInterface);
  43.         Debug.Assert(operationHR == HResult.S_OK);
  44.  
  45.         m_Result = (T)activatedInterface;
  46.  
  47.         var setResult = m_CompletionEvent.Set();
  48.         Debug.Assert(setResult != false);
  49.     }
  50.  
  51.     public T WaitForCompletion()
  52.     {
  53.         var waitResult = m_CompletionEvent.WaitOne();
  54.         Debug.Assert(waitResult != false);
  55.  
  56.         return m_Result;
  57.     }
  58.  
  59.     private AutoResetEvent m_CompletionEvent;
  60.     private T m_Result;
  61. }
  62.  
  63.  
  64. [ComImport]
  65. [Guid("5CDF2C82-841E-4546-9722-0CF74078229A")]
  66. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  67. unsafe interface IAudioEndpointVolume
  68. {
  69.     void RegisterControlChangeNotify(object pNotify);
  70.     void UnregisterControlChangeNotify(object pNotify);
  71.     uint GetChannelCount();
  72.     void SetMasterVolumeLevel(float fLevelDB, Guid* pguidEventContext);
  73.     void SetMasterVolumeLevelScalar(float fLevel, Guid* pguidEventContext);
  74.     float GetMasterVolumeLevel();
  75.     float GetMasterVolumeLevelScalar();
  76.     void SetChannelVolumeLevel(uint nChannel, float fLevelDB, Guid* pguidEventContext);
  77.     void SetChannelVolumeLevelScalar(uint nChannel, float fLevel, Guid* pguidEventContext);        
  78.     float GetChannelVolumeLevel(uint nChannel);
  79.     float GetChannelVolumeLevelScalar(uint nChannel);
  80.     void SetMute(bool bMute, Guid* pguidEventContext);
  81.     bool GetMute();
  82.     void GetVolumeStepInfo(out uint pnStep, out uint pnStepCount);
  83.     void VolumeStepUp(Guid* pguidEventContext);
  84.     void VolumeStepDown(Guid* pguidEventContext);
  85.     uint QueryHardwareSupport();
  86.     void GetVolumeRange(out float pflVolumeMindB, out float pflVolumeMaxdB, out float pflVolumeIncrementdB);        
  87. }
  88.  
  89. static class VolumeControl
  90. {
  91.     [DllImport("Mmdevapi.dll")]
  92.     [return: MarshalAs(UnmanagedType.Error)]
  93.     static extern HResult ActivateAudioInterfaceAsync(
  94.         [MarshalAs(UnmanagedType.LPWStr)]string deviceInterfacePath,
  95.         [MarshalAs(UnmanagedType.LPStruct)]Guid riid,
  96.         IntPtr activationParams,
  97.         IActivateAudioInterfaceCompletionHandler completionHandler,
  98.         out IActivateAudioInterfaceAsyncOperation activationOperation);
  99.  
  100.     public static unsafe void ChangeVolumeToLevel(float level)
  101.     {
  102.         var defaultAudioRenderDevice = MediaDevice.GetDefaultAudioRenderId(AudioDeviceRole.Default);
  103.         var activateAudioInterfaceCompletionHandler = new ActivateAudioInterfaceCompletionHandler<IAudioEndpointVolume>();
  104.  
  105.         IActivateAudioInterfaceAsyncOperation activateOperation;
  106.         var hr = ActivateAudioInterfaceAsync(defaultAudioRenderDevice, typeof(IAudioEndpointVolume).GetTypeInfo().GUID, IntPtr.Zero, (IActivateAudioInterfaceCompletionHandler)activateAudioInterfaceCompletionHandler, out activateOperation);
  107.         Debug.Assert(hr == HResult.S_OK);
  108.  
  109.         var audioEndpointVolume = activateAudioInterfaceCompletionHandler.WaitForCompletion();
  110.         Debug.Assert(audioEndpointVolume != null);
  111.  
  112.         audioEndpointVolume.SetMasterVolumeLevelScalar(level, null);
  113.     }
  114. }
  115.  
  116. class FrameworkView : IFrameworkView
  117. {
  118.     public void Initialize(CoreApplicationView applicationView)
  119.     {
  120.     }
  121.  
  122.     public void SetWindow(CoreWindow window)
  123.     {
  124.         Debug.Assert(window != null);
  125.         m_CoreWindow = window;
  126.     }
  127.  
  128.     public void Load(string entryPoint)
  129.     {
  130.         Debug.Assert(m_CoreWindow != null);
  131.         m_CoreWindow.Activate();
  132.     }
  133.  
  134.     public void Run()
  135.     {
  136.         VolumeControl.ChangeVolumeToLevel(0.5f);
  137.     }
  138.  
  139.     public void Uninitialize()
  140.     {
  141.         m_CoreWindow = null;
  142.     }
  143.  
  144.     private ICoreWindow m_CoreWindow;
  145. }
  146.  
  147. class FrameworkViewSource : IFrameworkViewSource
  148. {
  149.     public IFrameworkView CreateView()
  150.     {
  151.         return new FrameworkView();
  152.     }
  153. };
  154.  
  155. class Program
  156. {
  157.     static void Main()
  158.     {
  159.         CoreApplication.Run(new FrameworkViewSource());
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement