Learn how to play - pause music in windows (valid for all music players like spotify, itunes,windows media player) and go to next or previous track.

How to play, pause music or go to next and previous track from windows using C# (Valid for all windows music players)

You can use this snippet to stop the windows music, yes, if you use the following code you'll be able to stop (or jump to next and previous track) the music of windows. That means that any active music player we'll respond to the action. This is possible thanks to the simulation of a keypress of the system using the user32.dll and the keybd_event function.

Implementing

To start, we need to include the following namespace in the main class of our project.

using System.Runtime.InteropServices;

Then we are going to import into our project the user32.dll , this api will allow us to get acces to the keybd_event function which we will use to simulate a couple of key press in the system. Immediately, create the method too using :

[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);

Now we'll be able to simulate the key events that we need to play-pause-JumpToNextTrack or JumpToPreviousTrack, but we need to declarate too the in variables the codes of the keys that we want to simulate. Just add the following variables in the class (note that they need to be accesible for all the methods):

public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;// code to jump to next track
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;// code to play or pause a song
public const int VK_MEDIA_PREV_TRACK = 0xB1;// code to jump to prev track

To simulate events, use the following lines of code (everyline does something different, captain obvious) :

// Jump to previous track
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
// Play or Pause music
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
// Jump to next track
keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);

The keybd_event function expects as first parameter the code of the key that will be simulated. Now you just need to add a couple of buttons to your form and attach those functions to click events of the buttons.

A final implementation should work like a charm, the following gif shows the app working with Spotify music. A you may know this app doesn't have any spotify code or anything else, it will only simulate a keyboard event on the system. Therefore this function will work for any music player for windows.

Spotify pause music with c#

Complete class

Your class should look like the following when you add the buttons :

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

namespace UniversalSandbox
{
    public partial class Form1 : Form
    {
        public const int KEYEVENTF_EXTENTEDKEY = 1;
        public const int KEYEVENTF_KEYUP = 0;
        public const int VK_MEDIA_NEXT_TRACK = 0xB0;
        public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
        public const int VK_MEDIA_PREV_TRACK = 0xB1;

        [DllImport("user32.dll")]
        public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
        }
    }
}

Have fun !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors