Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...