2021-04-21 10:14:57 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
|
|
|
|
play-mp3.ps1 [<MP3-file>]
|
|
|
|
|
.DESCRIPTION
|
2021-08-29 17:50:03 +02:00
|
|
|
|
Plays the given sound file (MP3 file format).
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS> .\play-mp3.ps1 C:\thunder.mp3
|
2021-08-29 17:50:03 +02:00
|
|
|
|
.NOTES
|
|
|
|
|
Author: Markus Fleschutz · License: CC0
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
2021-02-16 20:03:32 +01:00
|
|
|
|
#>
|
|
|
|
|
|
2021-07-15 15:51:22 +02:00
|
|
|
|
param([string]$Filename = "")
|
2021-02-18 20:17:55 +01:00
|
|
|
|
|
2021-02-16 20:03:32 +01:00
|
|
|
|
try {
|
2021-07-15 15:51:22 +02:00
|
|
|
|
if ($Filename -eq "" ) { $Filename = read-host "Enter the MP3 filename" }
|
|
|
|
|
|
2021-04-30 20:39:06 +02:00
|
|
|
|
if (-not(test-path "$Filename" -pathType leaf)) { throw "Can't access audio file: $Filename" }
|
|
|
|
|
$FullPath = (get-childItem $Filename).fullname
|
|
|
|
|
$Filename = (get-item "$FullPath").name
|
2021-04-21 10:14:57 +02:00
|
|
|
|
|
2021-02-20 12:08:05 +01:00
|
|
|
|
add-type -assemblyName PresentationCore
|
|
|
|
|
$MediaPlayer = new-object System.Windows.Media.MediaPlayer
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
$MediaPlayer.open($FullPath)
|
2021-04-21 10:14:57 +02:00
|
|
|
|
$Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
|
|
|
|
|
} until ($Milliseconds)
|
2021-04-30 20:39:06 +02:00
|
|
|
|
|
2021-04-21 10:14:57 +02:00
|
|
|
|
[int]$Minutes = $Milliseconds / 60000
|
|
|
|
|
[int]$Seconds = ($Milliseconds / 1000) % 60
|
2021-05-07 15:41:21 +02:00
|
|
|
|
"▶️ Playing 🎵$Filename for $($Minutes.ToString('00')):$($Seconds.ToString('00')) sec..."
|
2021-05-06 16:19:50 +02:00
|
|
|
|
$PreviousTitle = $host.ui.RawUI.WindowTitle
|
2021-05-07 15:09:23 +02:00
|
|
|
|
$host.ui.RawUI.WindowTitle = "▶️ $Filename"
|
2021-02-20 12:08:05 +01:00
|
|
|
|
$MediaPlayer.Volume = 1
|
2021-02-17 20:09:44 +01:00
|
|
|
|
$MediaPlayer.play()
|
2021-04-21 10:14:57 +02:00
|
|
|
|
start-sleep -milliseconds $Milliseconds
|
2021-02-20 12:08:05 +01:00
|
|
|
|
$MediaPlayer.stop()
|
|
|
|
|
$MediaPlayer.close()
|
2021-05-06 16:19:50 +02:00
|
|
|
|
$host.ui.RawUI.WindowTitle = $PreviousTitle
|
2021-02-17 20:09:44 +01:00
|
|
|
|
|
2021-02-16 20:03:32 +01:00
|
|
|
|
exit 0
|
|
|
|
|
} catch {
|
2021-09-16 20:19:10 +02:00
|
|
|
|
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
2021-02-16 20:03:32 +01:00
|
|
|
|
exit 1
|
|
|
|
|
}
|