Home > Software design >  How can I enable input microphone when I select on the combo box
How can I enable input microphone when I select on the combo box

Time:08-22

I am making Speech to text application in C# window form with Microsoft Azure. I would like to have a combo box on my form which show the user enable microphone. When I select the enable microphone and press the ok button the microphone will be select as input which I selected. Which I have include in combo box which they would like to use.

How can I implement such a feature?

Code:

using System;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;
using Microsoft.CognitiveServices.Speech;

namespace WindowsFormsApp2
{
    public partial class Form2 : Form
    {
        [DllImport("winmm.dll", SetLastError = true)]
        static extern uint waveInGetNumDevs();

        [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint waveInGetDevCaps(uint hwo, ref WAVEOUTCAPS pwoc, uint cbwoc);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct WAVEOUTCAPS
        {
            public ushort wMid;
            public ushort wPid;
            public uint vDriverVersion;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            public string szPname;
            public uint dwFormats;
            public ushort wChannels;
            public ushort wReserved1;
            public uint dwSupport;
        }

        public void FillSoundDevicesInCombobox()
        {
            uint devices = waveInGetNumDevs();
            WAVEOUTCAPS caps = new WAVEOUTCAPS();
            for (uint i = 0; i < devices; i  )
            {
                waveInGetDevCaps(i, ref caps, (uint)Marshal.SizeOf(caps));
                CB1.Items.Add(caps.szPname);
            }
        }
      
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            FillSoundDevicesInCombobox();

            // Only select the device if there is a value to load
            if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.SelectedAudioDevice))
            {
                // find the device, matching on the value, not index
                var item = CB1.Items.Cast<string>().FirstOrDefault(x => x.Equals(Properties.Settings.Default.SelectedAudioDevice));
                // only select the device if we found one that matched the previous selection.
                if (item != null)
                    CB1.SelectedItem = item;
            }

            checkBox1.Checked = Properties.Settings.Default.Punctuation;
            //CB1.SelectedItem = Properties.Settings.Default.FillSoundDevicesInCombobox;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.SelectedAudioDevice = CB1.SelectedItem?.ToString();
            Properties.Settings.Default.Punctuation = checkBox1.Checked;
            //Properties.Settings.Default.FillSoundDevicesInCombobox = CB1.SelectedItem?.ToString();
            Properties.Settings.Default.Save();
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

CodePudding user response:

You can use use Lync2013SDK by installing below nuget package:

Install-Package Lync2013SDK -Version 15.0.4466.1000

Then call below function:

private void ComboBox1_SelectedIndexChanged(object sender,  System.EventArgs e)
{
    ComboBox comboBox = (ComboBox) sender;
    string selected = (string) ComboBox1.SelectedItem;
    int i = ComboBox1.FindStringExact(selected);

    var manager = LyncClient.GetClient().DeviceManager;
    manager.ActiveAudioDevice = (AudioDevice)manager.AudioDevices[i];   //           
  • Related