ps1.soapyfrog.com

doing inappropriate things with powershell

Pages

  • About
  • Downloads
  • Archives

Search

Popular Posts

  • Grrr 1.1 and Big Invaders
  • Space Invaders
  • Convert images to text (ASCII art)
  • Cmdlet clashes
  • Console screen grabs in html

Recent Posts

  • Grrr source code, including Invaders
  • Google going down the pan
  • Cmdlet clashes
  • Grrr 1.1 and Big Invaders
  • Grrr, Cmdlets and PSInvaders revival

Categories

  • Announce (7)
  • Cmdlets (2)
  • Cool (16)
  • Grrr (6)
  • Hint (2)
  • Invaders (5)
  • Odd (2)
  • PowerShell (27)
  • Quiz (3)
  • Rant (7)
  • Uncategorized (1)
  • Utility (5)

Months

  • August 2007 (1)
  • April 2007 (1)
  • March 2007 (1)
  • February 2007 (3)
  • January 2007 (25)
  • December 2006 (1)

Bookmarks

  • Blogroll
    • $script Fanatics
    • blog.soapyfrog.com
    • Brian Long
    • Lee Holmes
    • Nik Crabtree
    • PowerShell-Scripting (French)
    • Richy Rich
    • The PowerShell Guy
    • Windows PowerShell
  • Links
    • Carbon-neutral web hosting!

Meta

  • Log in
  • Posts RSS
  • Comments RSS
  • Valid XHTML
  • Valid CSS
« Script Updates
Space Invaders with sound! »
 

Making noises

11 Jan 2007 03:54 pm// Cool, PowerShell, Quiz    

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?

4 comments to “Making noises”

  1. 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

  2. On 12 Jan 2007 at 5:53 am, adrian said:   

    Thanks Greg - I suppose I should have thought about COM :-)

  3. 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\/\/

  4. 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.

Copyright © 2006-2008 Adrian Milliner

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.