Making noises
There are twothree ways I know of to make noises in PowerShell.
1. Simple and most boring: use the internal speaker’s beep.
write-host "`a`a`a"
2. Slightly better, use the .NET console beep. This plays A above middle C for one second.
[console]::beep(440,1000)
Both of the above are synchronous, ie, the script blocks until the sound has finished. Richy Rich is doing some interesting stuff with runspaces to do this asynchronously.
3. Lastly, you can play wav files. Sadly, only .wav files. No midi, mp3, mpeg4.. at least I couldn’t find anything in .net 2.
$file='C:\\WINDOWS\\Media\\Windows XP Startup.wav'
$sp = new-object Media.SoundPlayer ($file)
$sp.Load();
$sp.Play();
1..5 | % {
echo "la la"
sleep 1
}
This plays the wav asynchronously, so I feel should be used in a future version of psinvaders to get those authentic sounds.
So anyone know of a way to play midi files asynchronously in PowerShell with only a few lines of code?


On 11 Jan 2007 at 11:49 pm, gelyon said:
The following uses Windows Media Player, it will play midi files and anything else WMP can play.
function Play-Media( [String] $strMediaFilePath )
{
if( $strMediaFilePath )
{
$player = New-Object -ComObject “wmplayer.ocx”
$media = $player.NewMedia($strMediaFilePath)
[void] $player.CurrentPlaylist.AppendItem($media)
$player.Controls.Play()
}
}
Greg L
On 12 Jan 2007 at 5:53 am, adrian said:
Thanks Greg - I suppose I should have thought about COM
On 13 Jan 2007 at 6:47 pm, MoW said:
I also have 2 blogentries about using media player, make playlists and playing music in PowerShell (Monad at that time).
http://mow001.blogspot.com/2006/01/some-fun-with-monads-add-member-mp3.html
http://mow001.blogspot.com/2005/12/monad-really-does-rock.html
Enjoy
Greetings /\/\o\/\/
On 13 Jan 2007 at 8:41 pm, adrian said:
Cheers /\/\o\/\/, I was wondering how to create objects with arbitrary properties like in http://mow001.blogspot.com/2006/01/some-fun-with-monads-add-member-mp3.html
It does look a bit messy - I was hoping that an array of hashes would work - but this certainly solves some problems I was having.