none
Windows Soundausgabe festlegen/ändern RRS feed

  • Allgemeine Diskussion

  • Hallo Leute,

    ich bin auf der Suche nach einer Möglichkeit alle Audioausgabegeräte(Line-Out, Bluetooth, Speaker, HDMI, usw...) auf zu listen und nach beliebigen aktivieren/ändern.

     

    Hintergrund: wenn ich mein Laptop am Fernseher über HDMI anschließe, muss man in der Systemsteuerung / Sound / Wiedergabe, das gewünschte Gerät markieren und als Standard setzen. Ich würde das ganze gerne automatisieren.

    Im Netz habe ich schon mal gefunden, dass man das über DirectSound realisieren kann (zumindest die Soundgeräte auflisten). Leider war kein brauchbarer Code dabei. VB2010 würde auch gehen(bin grad dabei langsam zu wechseln)

    Hat einer nen Link(vielleicht suche ich einfach nur falsch) oder Beispielcode?  Danke  :-)

    Freitag, 1. April 2011 12:04

Alle Antworten

  • Hallo Philipp B. _,

    Im folgenden Code findest du eine Möglichkeit alle Audioausgabegeräte auf zu listen über DirectSound (VB.NET und C# Code)

    [VB.NET]

    Imports System.Runtime.InteropServices
    
    Module Module1
      Private Declare Function DirectSoundEnumerate Lib "dsound.dll" Alias "DirectSoundEnumerateA" (ByVal lpDSEnumCallback As DSCallback, ByVal lpContext As Integer) As Integer
    
      Delegate Function DSCallback(ByVal lpGuid As Integer, ByVal lpcstr As Integer, ByVal lpcstrModule As Integer, ByVal lpContext As Integer) As Boolean
    
      Sub main()
        Dim x As Long
        x = DirectSoundEnumerate(New DSCallback(AddressOf PopulateSoundDevices), IntPtr.Zero)
      End Sub
    
    
      Public Function PopulateSoundDevices(ByVal lpGuid As Integer, ByVal lpcstrDescription As Integer, ByVal lpcstrModule As Integer, ByVal lpContext As Integer) As Boolean
        Console.WriteLine(Marshal.PtrToStringAnsi(lpcstrDescription))
        PopulateSoundDevices = True
      End Function
    
    
    End Module
    

     

    [C#]

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        /// <summary>
        /// The DirectSoundEnumerate function enumerates the DirectSound drivers installed in the system.
        /// </summary>
        /// <param name="lpDSEnumCallback">callback function</param>
        /// <param name="lpContext">User context</param>
        [DllImport("dsound.dll", EntryPoint = "DirectSoundEnumerateA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        static extern void DirectSoundEnumerate(DSEnumCallback lpDSEnumCallback, IntPtr lpContext);
    
        /// <summary>
        /// The DSEnumCallback function is an application-defined callback function that enumerates the DirectSound drivers. 
        /// The system calls this function in response to the application's call to the DirectSoundEnumerate or DirectSoundCaptureEnumerate function.
        /// </summary>
        /// <param name="lpGuid">Address of the GUID that identifies the device being enumerated, or NULL for the primary device. This value can be passed to the DirectSoundCreate8 or DirectSoundCaptureCreate8 function to create a device object for that driver. </param>
        /// <param name="lpcstrDescription">Address of a null-terminated string that provides a textual description of the DirectSound device. </param>
        /// <param name="lpcstrModule">Address of a null-terminated string that specifies the module name of the DirectSound driver corresponding to this device. </param>
        /// <param name="lpContext">Address of application-defined data. This is the pointer passed to DirectSoundEnumerate or DirectSoundCaptureEnumerate as the lpContext parameter. </param>
        /// <returns>Returns TRUE to continue enumerating drivers, or FALSE to stop.</returns>
        delegate bool DSEnumCallback(IntPtr lpGuid, IntPtr lpcstrDescription, IntPtr lpcstrModule, IntPtr lpContext);
        public static List<DirectSoundDeviceInfo> devices;
    
        public static bool EnumCallback(IntPtr lpGuid, IntPtr lpcstrDescription, IntPtr lpcstrModule, IntPtr lpContext)
        {
          //List<DirectSoundDeviceInfo> devices=new List<DirectSoundDeviceInfo>();
          var device = new DirectSoundDeviceInfo();
          if (lpGuid == IntPtr.Zero)
          {
            device.Guid = Guid.Empty;
          }
          else
          {
            byte[] guidBytes = new byte[16];
            Marshal.Copy(lpGuid, guidBytes, 0, 16);
            device.Guid = new Guid(guidBytes);
          }
          device.Description = Marshal.PtrToStringAnsi(lpcstrDescription);
          if (lpcstrModule != null)
          {
            device.ModuleName = Marshal.PtrToStringAnsi(lpcstrModule);
          }
          //devices.Add(device);
          devices.Add(device);
          return true;
        }
    
        /// <summary>
        /// Class for enumerating DirectSound devices
        /// </summary>
        public class DirectSoundDeviceInfo
        {
          /// <summary>
          /// The device identifier
          /// </summary>
          public Guid Guid { get; set; }
          /// <summary>
          /// Device description
          /// </summary>
          public string Description { get; set; }
          /// <summary>
          /// Device module name
          /// </summary>
          public string ModuleName { get; set; }
        }
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          devices = new List<DirectSoundDeviceInfo>();
          DirectSoundEnumerate(new DSEnumCallback(EnumCallback), IntPtr.Zero);
          listBox1.DataSource = devices;
          listBox1.DisplayMember = "Description";
        }
      }
    }
    

     

    Grüße,

    Robert

    Montag, 23. Mai 2011 06:47
    Moderator