PowerShell/Scripts/play-mp3.ps1

36 lines
889 B
PowerShell
Raw Normal View History

2021-04-07 11:53:57 +02:00
#!/usr/bin/pwsh
2021-02-16 20:03:32 +01:00
<#
2021-03-22 20:10:18 +01:00
.SYNTAX ./play-mp3.ps1 [<MP3-file>]
.DESCRIPTION plays the given sound file (MP3 file format)
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
2021-02-16 20:03:32 +01:00
#>
param($Filename = "")
2021-02-18 20:17:55 +01:00
if ($Filename -eq "" ) {
$Filename = read-host "Enter the MP3 filename"
}
2021-02-16 20:03:32 +01:00
try {
2021-02-20 12:08:05 +01:00
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 ..."
$MediaPlayer.Volume = 1
2021-02-17 20:09:44 +01:00
$MediaPlayer.play()
2021-02-20 12:08:05 +01:00
start-sleep -milliseconds $Duration
$MediaPlayer.stop()
$MediaPlayer.close()
2021-02-17 20:09:44 +01:00
2021-02-16 20:03:32 +01:00
exit 0
} catch {
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}