How to check if an audio file is being played by programs like video players or audio players using C# as console-based app?

Rahmatulloh 40 Reputation points
2024-06-04T15:43:31.9966667+00:00

I want to check if an audio file is being played by audio or video player programs or by other programs.

Since I don't have any knowledge or somehow solid knowledge about how to do it, I hope, you all(Microsoft Q&A community) will help me to do this kind of thing in C# as a console-based app.(As far as I know, perhaps we should use WASAPI)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2024-06-05T03:18:33.8+00:00

    Hi @Rahmatulloh , Welcome to Microsoft Q&A,

    Use the NAudio library to get the volume of the current system audio playback device. If the volume is greater than zero, it can be determined that audio is playing.

    Use the Windows API to get all running processes in the system and check the audio output of these processes.

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using NAudio.CoreAudioApi;
    
    class Program
    {
        static void Main(string[] args)
        {
            var deviceEnumerator = new MMDeviceEnumerator();
            var defaultDevice = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
    
            // Get the audio meter information
            var audioMeterInformation = defaultDevice.AudioMeterInformation;
    
            // Check if the audio level is greater than 0 (indicating that audio is playing)
            if (audioMeterInformation.MasterPeakValue > 0)
            {
                Console.WriteLine("Audio is playing.");
            }
            else
            {
                Console.WriteLine("No audio is playing.");
            }
    
            // Optionally, you can list all processes and check their names
            var processes = Process.GetProcesses();
            foreach (var process in processes)
            {
                Console.WriteLine($"Process: {process.ProcessName} (ID: {process.Id})");
            }
    
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. KOZ6.0 6,300 Reputation points
    2024-06-06T07:14:53.24+00:00

    You can use handle.exe to find out what files a process has open.

    Handle v5.0 https://learn.microsoft.com/en-us/sysinternals/downloads/handle

    For example, to find out what files a media player has open, run the following command with administrator privileges. (grep will become available after installing gnuwin.)

    > handle -p Microsoft.Media.Player | grep -E "\.avi|\.mp3|\.mp4|\.mkv"
    E0C: File (RWD) C:\Temp\test.avi
    E24: File (RWD) C:\Temp\test.avi
    E30: File (RWD) C:\Temp\test.avi
    

    However, this only tells you that the file is open, not that it is playing.