Windows: Detecting Spotify track changes for notifications

I’m trying to create a Windows app that shows notifications when Spotify changes tracks. I’ve looked into toastify but it’s not working on my PC. Right now, I’m using a Python library to get the current song info.

The main issue is figuring out how to detect when Spotify moves to the next song on Windows. I’ve seen some code that uses Command IDs to control Spotify:

PLAY_PAUSE = 917504
STOP = 851968
PREV_TRACK = 786432
NEXT_TRACK = 720896

What tool do I need to find these IDs? And how can I get an ID for the ‘song changed’ event? Or am I approaching this the wrong way?

I know Linux folks use dbus for this kind of thing, but I’m not sure what the Windows equivalent would be. Any help would be great!

have u tried using the spotify web api? it’s got endpoints for getting the current track and stuff. you could poll it every few seconds to check if the song changed. might be easier than messing with windows-specific stuff. just need to set up auth and use requests library

While the Web API approach is viable, it might not be the most efficient for real-time tracking. Have you considered using the Windows API? Specifically, you could leverage the ISpTitleInfo interface from the Spotify COM API. This allows you to register for callbacks when the track changes, eliminating the need for constant polling. You’ll need to use ctypes or win32com in Python to interact with COM objects. It’s a bit more complex initially, but provides a more robust and responsive solution for track change detection on Windows. Just be aware that this method requires Spotify to be running as a desktop application, not in a browser.

yo, have u looked into using the win32gui library? it can help u snag window titles. u could set up a loop to check spotify’s window title every second or so. when it changes, boom, new song. might be simpler than messing with APIs or COM stuff. just a thought!

I’ve actually tackled a similar project before, and I found that using the Windows COM API was the most reliable method. Here’s what worked for me:

Instead of trying to find specific command IDs, I used the ISpTitleInfo interface from the Spotify COM API. This lets you hook directly into Spotify’s track change events.

To implement this, you’ll need to use either ctypes or win32com in Python. It’s a bit tricky to set up at first, but once you get it working, it’s rock solid. The key is to create a callback function that Spotify will trigger whenever the track changes.

One caveat: this only works with the desktop app, not the web player. But for a Windows-specific solution, it’s hard to beat.

If you’re struggling with the COM API implementation, I’d be happy to share some sample code that might help you get started. It’s definitely more complex than polling an API, but the real-time responsiveness is worth it in my opinion.

I’ve had success using the Windows Media Session API for this kind of task. It’s a bit lower-level than some other solutions, but it gives you precise control and real-time updates.

The key is to create a session manager and register for notifications. You can then receive callbacks when the track changes, without needing to constantly poll or check window titles.

It does require some C++ knowledge to implement, or you’d need to use Python’s ctypes to interface with the Windows API. There’s a bit of a learning curve, but it’s quite powerful once you get it set up.

One advantage is that this method works across different media players, not just Spotify. So if you ever want to expand your app’s functionality, you’re already set up for it.

Just be prepared for some debugging - Windows APIs can be finicky sometimes.