retrieve currently playing mediaon windows in csharp

To retrieve currently playing media on Windows in C#, you can use the Windows Media Player API. Here's a code snippet that demonstrates how to retrieve the current media being played:

main.cs
using System;
using WMPLib;

namespace myNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create new Windows Media Player instance
            WindowsMediaPlayer wmp = new WindowsMediaPlayer();

            // Retrieve the currently playing media
            IWMPMedia media = wmp.currentMedia;

            // Check if there is currently playing media
            if (media != null)
            {
                Console.WriteLine("Currently playing media:");
                Console.WriteLine("Title: " + media.name);
                Console.WriteLine("Artist: " + media.getItemInfo("Author"));
                Console.WriteLine("Album: " + media.getItemInfo("Album"));
            }
            else
            {
                Console.WriteLine("No media currently playing.");
            }
        }
    }
}
867 chars
31 lines

Note that you will need to add a reference to the "Windows Media Player" COM component in your C# project in order to use the WMPLib namespace. You can do this by right-clicking on your project in Visual Studio, selecting "Add > Reference...", and navigating to "Windows Media Player" in the COM tab.

gistlibby LogSnag