Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a sound is playing using c#?

Tags:

c#

audio

I have a TTS program (Third Party) and I wrote a c# application that uses that program. (Type into my application and press a button to move the mouse and click on the Third party app).

I need to know whether the speech is finished or not. Are there any ideas on how to determine if any sound playing from sound card or not?

like image 662
mefmef Avatar asked Dec 12 '22 05:12

mefmef


1 Answers

You could use CSCore.
Download it right here -> https://github.com/filoe/cscore

Paste these lines on a console project.

using System;
using CSCore.CoreAudioAPI;

namespace AudioDetector
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(IsAudioPlaying(GetDefaultRenderDevice()));
            Console.ReadLine();
        }

        public static MMDevice GetDefaultRenderDevice()
        {
            using (var enumerator = new MMDeviceEnumerator())
            {
                return enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
            }
        }

        public static bool IsAudioPlaying(MMDevice device)
        {
            using (var meter = AudioMeterInformation.FromDevice(device))
            {
                return meter.PeakValue > 0;
            }
        }
    }
}

Play a music be it on YouTube, Music Player, etc...
Run the program.
It automatically notifies(true/false) if there is an audio currently being played or not.

like image 183
Kent Aguilar Avatar answered Dec 22 '22 01:12

Kent Aguilar