Shuffle-play your audio files with PowerShell
Another little script to do something that might be a little tough otherwise...
WinAMP and WMP (Windows Media Player) both have shuffle functions, but I've wanted a way to do this from the command line for a while now...someone on a mailing list asked for a way to random play so I wrote a little script for it.
You can't use WMPlayer.exe for this because it detaches from the calling shell to become a true GUI-only application. I was unable to determine a set of command line switches that would make either WMP 10 or WMP 11 behave as I wanted.
MPlayer is a GPL'ed player found on Linux/Mac/Windows. It has options galore. The Windows port ships with two programs that provide GUI interfaces to the MPlayer executable. A simple one - MPUI - and a complex one - SMPlayer. I can already tell that I may be using MPlayer instead of WMP in the future.
You can get MPlayer for Windows here:
http://www.softpedia.com/progDownload/MPlayer-for-Windows-Full-Package-Download-55997.html
(and, I'm sure, from many other places)
Enough talking, here is the script:
#
# shuffle-play.ps1
#
# Michael B. Smith
# http://TheEssentialExchange.com
#
# MPlayer is available from
# http://www.softpedia.com/progDownload/MPlayer-for-Windows-Full-Package-Download-55997.html
#
Param(
$directory = "C:\Music",
$extensions = ("*.mp3", "*.wma")
)
$files = get-childitem $directory -name:$true -include $extensions
if ($files.Length -le 0)
{
write-host "No files meet spec"
return
}
$random = new-object System.Random
while (1)
{
$index = $random.Next(0, $files.Length - 1)
$media = '"' + $directory + "\" + $files[$index] + '"'
write-host $media
&"C:\Program Files\MPlayer for Windows\MPlayer.exe" "$media" | out-null
}