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