'How do I control volume with Speech Recognition in C#?

So, I've made my own speech recognition program in C# because I felt the Windows default program was a bit limited.

I'm not sure how I would be able to control system volume though. Any ideas?



Solution 1:[1]

This is a duplicate, but I'll answer anyway. Here is the code.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Test
{
    public partial class Form1 : Form
    {
        private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
        private const int APPCOMMAND_VOLUME_UP = 0xA0000;
        private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
        private const int WM_APPCOMMAND = 0x319;

        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
            IntPtr wParam, IntPtr lParam);

        public Form1()
        {
            InitializeComponent();
        }

        private void btnMute_Click(object sender, EventArgs e)
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_MUTE);
        }

        private void btnDecVol_Click(object sender, EventArgs e)
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_DOWN);
        }

        private void btnIncVol_Click(object sender, EventArgs e)
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_UP);
        }
    }
}

Copied from: dotnetcurry.

Solution 2:[2]

Clearly, Omni didn't understand the question asked by the OP. The OP asked, "How do I control volume with Speech Recognition"...Not "How do I control volume with buttons". This is how you properly do it and I am sorry an answer wasn't provided sooner to you.

Step 1: Go to "Project" and select Add Reference. Under Assemblies, add "System.Speech" Next: Use the following code: (This is my own work from my own project at: https://github.com/DeadReport77/Scarlett-Speech-Recognition-Final-Production/commit/20f4db5d3b75b03d601d39c6fe54e8261c7976f0). Other commands can be found there for web page control etc. In this demonstration, I am using a loop that loops 10 times. That number can be changed to any number ranging from 1-100, to fit your needs. If you have further questions about this or wish to add to your project, please use my email: [email protected].

Thank you for using Stackoverflow.

///Created by Justin Linwood Ross (United States-Maine)///
///Scarlett Centuri Model A-7 (© Copyright 2021)///
using System;
using System.Diagnostics;
using System.Media;
using System.Runtime.InteropServices;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Threading;
using System.Windows.Forms;

namespace Scarlett
{


    public partial class Form1 : Form
    {
        private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
        private const int APPCOMMAND_VOLUME_UP = 0xA0000;
        private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
        private const int WM_APPCOMMAND = 0x319;
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
            IntPtr wParam, IntPtr lParam);
       
        SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();

        public Form1()
        {
            InitializeComponent();
           
        }

       
        private void Form1_Load(object sender, EventArgs e)
        {
            Choices commands = new Choices();
            commands.Add(new string[] { "scarlett mute volume", "scarlett volume up", "scarlett volume down"});
            GrammarBuilder gramBuilder = new GrammarBuilder();
            gramBuilder.Append(commands);
            Grammar grammar = new Grammar(gramBuilder);
            Grammar gram = grammar;
            recEngine.LoadGrammarAsync(gram);
            recEngine.SetInputToDefaultAudioDevice();
            recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
            recEngine.RecognizeAsync(RecognizeMode.Multiple);
           
        }


        private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text)
            {
                case "scarlett mute volume":
                    SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_MUTE);
                    break;

               //The volume will jump up 10% each time using a loop.
                case "scarlett volume up":
                    int repeat = 10; //Set loop for any # 1-100
                    for (int i = 0; i < repeat; i++)
                        SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                    (IntPtr)APPCOMMAND_VOLUME_UP);
                    break;

                 //The volume will jump down 10% each time.
                //Set loop (10;) for any # 1-100
                case "scarlett volume down":
                    for (int iter = 0; iter < 10; iter++)
                        SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
               (IntPtr)APPCOMMAND_VOLUME_DOWN);
                    break;
                                       
                        
            }
        }
      
    }
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 omni
Solution 2 Rythorian77