Skip to content

Instantly share code, notes, and snippets.

@megadix
Last active November 27, 2019 08:38
Show Gist options
  • Save megadix/05e2b8d6a649196ef9ab692dd186970f to your computer and use it in GitHub Desktop.
Save megadix/05e2b8d6a649196ef9ab692dd186970f to your computer and use it in GitHub Desktop.
PowerShell: enqueue and play a random song with VLC
function playMusicForProgramming() {
param(
[int] $n = 1
)
playRandomMusic $n 'C:\download\music\musicforprogramming.net'
}
function playAmbientMusic() {
param(
[int] $n = 1
)
playRandomMusic $n 'C:\download\music\ambient' -recurse -folders
}
function playRandomMusic() {
param(
[int] $n = 1,
[string] $folder = 'C:\download\music',
[Switch] $recurse = $true,
[Switch] $folders = $false
)
$first = $true;
For ($i = 1; $i -le $n; $i++) {
if ($folders) {
$file = dir -Path "${folder}\*" -Directory -Recurse:$recurse
}
else {
$excludes = ("*.jpg", "*.pdf", "*.txt");
$file = dir -Path "${folder}\*" -File -Recurse:$recurse -Exclude $excludes
}
$file = $file | Get-Random
echo "Enqueueing to playlist: ${file}"
$cmd = 'C:\Program Files\VideoLAN\VLC\vlc.exe'
$params = '--one-instance', '--playlist-enqueue', $file.FullName
& $cmd $params
if ($first) {
# Allows VLC to start playing first song
Start-Sleep -s 1
$first = $false;
}
}
}
@megadix
Copy link
Author

megadix commented Nov 7, 2018

You can put this function in your PowerShell profile, so any time you want some music to play, you can issue one of these commands on PS terminal:

playMusicForProgramming
playAmbientMusic
playRandomMusic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment