yah - I have an entire script that adjusts volume, etc. I've put it in the
book I'm writing
ok - here's the whole thing - it uses my convert-hashtoobject cmdlet, which
if you don't have, you can replace with this:
function convert-hashtoobject
{
param ( [hashtable]$hashtable, $names )
$o = new-object psobject
$o.psobject.typenames[0] = $names
foreach($key in $hashtable.keys) { $o|add-member noteproperty $key
$hashtable.$key }
$o
}
jim
#### Play-Media.ps1
param ( [switch]$Next, [switch]$Prev,
[switch]$Pause, [switch]$Stop, [switch]$Start, [int]$Number,
[switch]$Louder, [switch]$Softer, [int]$volume,
[switch]$Playlist, [switch]$list, [string[]]$Add, [switch]$Clear,
[switch]$Current, [int]$Skip,
[switch]$raw,
[switch]$Quit
)
if ( $args )
{
@"
USAGE: $( $myinvocation.mycommand.name )
A COM based media player. It creates a global variable `$player
which is used to play files without starting media player.
Options:
-Next Go to next song in songlist
-Prev Go to previous song in songlist
-Pause Pause playback
-Stop Stop playback
-Start Start playback
-Number <num> Go to <number> in playlist
-Louder Raise the volume
-Softer Lower the volume
-Playlist(list) Show the playlist
-Add <string> Add a file to the playlist
-Clear Clear the playlist
-Current Show the current playing file
-quit Quit and remove `$player
"@
exit
}
if ( ! (get-command Convert-HashToObject 2> $null) )
{
add-pssnapin ConversionCmdlets 2> $null
if ( ! (get-command Convert-HashToObject 2> $null) )
{
ConversionCmdlets not loaded, exiting
exit;
}
}
if ( ! $global

layer )
{
$host.ui.rawui.windowtitle = "PS(P)> $PWD"
$global

layer = new -com wmplayer.ocx
}
$fmt = @{ e = "Artist"; W = 20 },"Name",@{e ="Duration"; W = 6}
function play-number ( $number )
{
$number--
trap { continue }
$item = $player.currentplaylist.item($number)
if ( $item )
{
$player.controls.stop()
$player.controls.currentitem = $item
$player.controls.play()
}
else
{
write-host -fore red "No such number $number"
exit
}
}
if ( $next ) { $player.controls.next() }
elseif ( $prev ) { $player.controls.previous() }
elseif ( $stop ) { $player.controls.stop() }
elseif ( $start ) { $player.controls.play() }
elseif ( $pause ) { $player.controls.pause() }
elseif ( $louder ) { $player.settings.volume += 10 }
elseif ( $softer ) { $player.settings.volume -= 10 }
elseif ( $volume ) { $player.settings.volume = $volume }
elseif ( $clear ) { $player.currentplaylist.clear() }
elseif ( $number ) { play-number $number }
elseif ( $Skip ) { $player.controls.currentposition += $Skip }
elseif ( $playlist -or $list )
{
$count = $player.currentplaylist.count - 1
if ( $count -lt 0 )
{
"Playlist is empty"
exit
}
$numberInPlaylist = @{
A = "Right"; L = "Num"; w = 4
e = { $_.Num }
}
$fmt = @( $numberInPlayList ) + $fmt
$url = $player.currentmedia.sourceurl
if ( $raw )
{
0..$count | foreach { $player.currentplaylist.item($_) }
return
}
0..($count) |
foreach {
[timespan]$total = 0 ; [int]$fnumber = 1
} {
$iam = $player.currentplaylist.item($_)
if ( $iam.sourceurl -eq $url )
{
$fg = $host.ui.rawui.foregroundcolor
$host.ui.rawui.foregroundcolor = "red"
}
Convert-HashToObject -names Playlist.Entry -hash @{
Artist = $iam.getiteminfo("Artist")
Duration = $iam.DurationString
Name = $iam.Name
Num = $fnumber
}
if ( $iam.sourceurl -eq $url )
{
$host.ui.rawui.foregroundcolor = $fg
}
$fnumber++
$total += ("00:" + $iam.durationstring)
} { write-host " Total time: $total" } | format-table $fmt
}
elseif ( $current )
{
if ( $raw )
{
$player.currentmedia
return
}
$lfmt = $fmt + @{
W = 5
L = "Pos"
E = {
$ts = new timespan 1,0,$player.controls.currentposition
"{0}:{1:00}" -f $ts.minutes,$ts.seconds
}
}
$o = Convert-HashToObject -names Playlist.CurrentEntry -hash @{
Artist = $player.currentmedia.getiteminfo("Artist")
Duration = $player.currentmedia.DurationString
Name = $player.currentmedia.Name
}
$o | format-table $lfmt
}
elseif ( $add )
{
foreach($mediapath in $add )
{
# make sure we have a real path
$path = (resolve-path $mediapath).path
# open a playlist and add the elements to our playlist
if ( $path -match "m3u$" )
{
get-content $path | where { $_ -notmatch "^#" } |
foreach {
$player.currentplaylist.appenditem(($player.newmedia($_))) }
}
else
{
$player.currentplaylist.appenditem(($player.newmedia($path)))
}
}
$player.controls.play()
}
elseif ( $quit )
{
$player.controls.stop()
$player.close()
remove-item variable

layer
}
--
--
James Truher [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
"Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
news:OfLh0zW7GHA.1188@TK2MSFTNGP05.phx.gbl...
> Thanks, Jim. I never worked that out from the WMP docs - although I didn't
> dig as deeply as I should have. :|
>
> I also didn't realize that it would be possible to play multiple items in
> parallel. Here's a quick wrapper for anyone who just wants to queue up an
> item in PS.
>
> function Start-Media
> {
> Param([string]$FilePath)
> $player = New-Object -ComObject wmplayer.ocx
> $player.currentplaylist.appenditem(($player.newmedia($FilePath)))
> $player.controls.play()
> }
>
>
> "James Truher" <jimtru@news.microsoft.com> wrote in message
> news:u7m8z1V7GHA.2384@TK2MSFTNGP04.phx.gbl...
>> I've been using a script to play my media files for a while, it builds
>> from here:
>>
>> $path = "$env:windir\media\tada.wav"
>> $player = new-object -com wmplayer.ocx
>> $player.currentplaylist.appenditem(($player.newmedia($path)))
>> $player.controls.play()
>>
>> --
>> --
>> James Truher [MSFT]
>> Windows PowerShell Development
>> Microsoft Corporation
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>> "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
>> news:OwxZiLV7GHA.4620@TK2MSFTNGP02.phx.gbl...
>>> "James Truher" <jimtru@news.microsoft.com> wrote in message
>>> news:OVVo2aa6GHA.348@TK2MSFTNGP02.phx.gbl...
>>>> here's a little mozart for you:
>>>
>>> On a tangent from this topic - any thoughts on a good approach to
>>> handling sound in PowerShell? We can do rather elegant beeping and even
>>> play wav files easily, but I haven't seen any simple way of playing back
>>> MDI or MP3.
>>>
>>>
>>
>>
>
>