Improve output of play-mp3.ps1

This commit is contained in:
Markus Fleschutz 2021-04-21 10:14:57 +02:00
parent e99cf0d32b
commit 94455bb7c5

View File

@ -1,5 +1,4 @@
#!/usr/bin/pwsh <#
<#
.SYNTAX play-mp3.ps1 [<MP3-file>] .SYNTAX play-mp3.ps1 [<MP3-file>]
.DESCRIPTION plays the given sound file (MP3 file format) .DESCRIPTION plays the given sound file (MP3 file format)
.LINK https://github.com/fleschutz/PowerShell .LINK https://github.com/fleschutz/PowerShell
@ -7,24 +6,25 @@
#> #>
param($Filename = "") param($Filename = "")
if ($Filename -eq "" ) { $Filename = read-host "Enter the MP3 filename" }
if ($Filename -eq "" ) {
$Filename = read-host "Enter the MP3 filename"
}
try { try {
$Filename = resolve-path -path "$Filename" -relative
add-type -assemblyName PresentationCore add-type -assemblyName PresentationCore
$MediaPlayer = new-object System.Windows.Media.MediaPlayer $MediaPlayer = new-object System.Windows.Media.MediaPlayer
$FullPath = (get-childItem $Filename).fullname $FullPath = (get-childItem $Filename).fullname
do { do {
$MediaPlayer.open($FullPath) $MediaPlayer.open($FullPath)
$Duration = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds $Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
} until ($Duration) } until ($Milliseconds)
write-progress "Playing $Filename ..." [int]$Minutes = $Milliseconds / 60000
[int]$Seconds = ($Milliseconds / 1000) % 60
"Playing 🎵$Filename ($($Minutes):$Seconds) ..."
$MediaPlayer.Volume = 1 $MediaPlayer.Volume = 1
$MediaPlayer.play() $MediaPlayer.play()
start-sleep -milliseconds $Duration start-sleep -milliseconds $Milliseconds
$MediaPlayer.stop() $MediaPlayer.stop()
$MediaPlayer.close() $MediaPlayer.close()