Uncategorized

Playing a song as a background process in Windows

Sometimes you ask yourself how to do cool things like playing a song in the background (ie. no visible interface or application) upon login on a Windows box. Being completely unfamiliar with using DOS I wasn’t quite sure how to go about doing this, but apparently it was quite easy. So here I am documenting it for future "reference". This marks my very first time touching the DOS prompt and indeed any sort of commands in Windows, so please excuse the newbie-format of this post.

Everything is done CLI for obvious reasons – we don’t want any interface for them to turn off our song. So we need a command line music player. mplayer is also available as a command line player on Windows, and so it was my first choice. A quick download of a build without an interface and we were ready to play any song with a *.bat file containing `mplayer "music.mp3"`

The next step is to make it run without the prompt opening up. This is again easily done by executing the bat file via a vbs file with the following content. Creating a shortcut to this vbs file and dumping it in your startup folder is the simplest and most obvious way to make it play on login. Here’s the code:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\path\to\my\bat\file.bat" & Chr(34), 0
Set WshShell = Nothing

Now I wanted to be able to change this song whenever I wanted from a central server. Basically it would check whether or not it needs to update the song, and if it does, delete the existing song and download the new song. This is useful to give a little variety in our fun little player. Some things didn’t work quite as I wanted it to so I have probably used the most horrendous of hacks based on what I could garner from various online references.

First I needed a way to download files akin to wget. I found a small program called url2file which did just the thing. I wanted it to check whether or not a song existed on the server, and if it did, download it. However the url2file program didn’t quite play nice with that idea (it would download a 404 page instead of allowing me to tell it not to do anything), and I didn’t know how to check whether or not a file existed on a remote server. So instead I had to make do with a second "notifier" file which, if it contained a certain string, would mean that a new song was available to be downloaded.

It would download that plaintext file’s contents to a tmp file, search in that tmp file for the string we were looking for, and if successful, would delete the existing music file and download the new one to take its place. Unfortunately doing a simple `if %getnew%==yes` didn’t work (explanations welcome!), so I made do with checking the first 3 characters, which did work. Here’s the final code, with the getnew.txt file including just the single word "yes".

del tmp
URL2FILE.EXE http://foobar.com/getnew.txt > tmp
set /p getnew= < tmp
set _part_name=%getnew:~0,3%
if %_part_name%==yes del music.mp3
if %_part_name%==yes URL2FILE.EXE http://foobar.com/music.mp3 music.mp3

Tada, and worked flawlessly. Not bad for a couple hours work from scratch and not knowing anything about DOS at all.

In unrelated news, I’m looking for good bagpipe music.

Dion Moult

I've been developing software for well over 10 years, work as an architect (not the computer kind, the regular sort), and am classically trained as a pianist. I try to do the right thing when I get the chance in my field, such as through contributing to open-source communities and promoting sustainable living.

More Posts

Spread the love
Tagged: , , , , , , ,

3 Comments

  1. Of course :) However this way everything can be self-contained within one directory and easily copied and “invisibly” set up on random people’s computers (thumbdrive + autorun and they’re none the wiser!).

    A fun harmless prank that will leave the less literate stumped.

Leave a Reply

Your email address will not be published. Required fields are marked *