2024-11-08 12:38:20 +01:00
|
|
|
The *play-mp3.ps1* Script
|
|
|
|
===========================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
This PowerShell script plays the given sound file (MPEG-1 audio layer-3 file format).
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2024-11-08 12:35:11 +01:00
|
|
|
/home/markus/Repos/PowerShell/scripts/play-mp3.ps1 [[-path] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
-path <String>
|
|
|
|
Specifies the file path to the .MP3 file
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2024-05-19 10:25:56 +02:00
|
|
|
PS> ./play-mp3.ps1 C:\thunder.mp3
|
2021-11-08 21:36:42 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-05-19 10:25:56 +02:00
|
|
|
Plays a .MP3 sound file
|
2022-11-17 20:02:26 +01:00
|
|
|
.DESCRIPTION
|
2024-05-19 10:25:56 +02:00
|
|
|
This PowerShell script plays the given sound file (MPEG-1 audio layer-3 file format).
|
|
|
|
.PARAMETER path
|
|
|
|
Specifies the file path to the .MP3 file
|
2022-11-17 20:02:26 +01:00
|
|
|
.EXAMPLE
|
2024-05-19 10:25:56 +02:00
|
|
|
PS> ./play-mp3.ps1 C:\thunder.mp3
|
2022-11-17 20:02:26 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
param([string]$path = "")
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
try {
|
2024-05-19 10:25:56 +02:00
|
|
|
if ($path -eq "" ) { $path = Read-Host "Enter the file path to the MP3 sound file" }
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-05-19 10:25:56 +02:00
|
|
|
if (-not(Test-Path "$path" -pathType leaf)) { throw "Can't access sound file: $path" }
|
|
|
|
$fullPath = (Get-ChildItem $path).fullname
|
|
|
|
$filename = (Get-Item "$fullPath").name
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2023-05-26 12:20:18 +02:00
|
|
|
Add-Type -assemblyName PresentationCore
|
2024-05-19 10:25:56 +02:00
|
|
|
$mediaPlayer = New-Object System.Windows.Media.MediaPlayer
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
do {
|
2024-05-19 10:25:56 +02:00
|
|
|
$mediaPlayer.open($fullPath)
|
|
|
|
$milliseconds = $mediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
|
|
|
|
} until ($milliseconds)
|
|
|
|
|
|
|
|
[int]$minutes = $milliseconds / 60000
|
|
|
|
[int]$seconds = ($milliseconds / 1000) % 60
|
|
|
|
Write-Host " ▶️" -noNewline -foregroundColor green
|
|
|
|
Write-Host "Playing $filename for $($minutes.ToString('00')):$($seconds.ToString('00'))s..."
|
|
|
|
|
|
|
|
$previousTitle = $host.ui.RawUI.WindowTitle
|
|
|
|
$host.ui.RawUI.WindowTitle = "▶️ $filename"
|
|
|
|
$mediaPlayer.Volume = 1
|
|
|
|
$mediaPlayer.play()
|
|
|
|
Start-Sleep -milliseconds $milliseconds
|
|
|
|
|
|
|
|
$mediaPlayer.stop()
|
|
|
|
$mediaPlayer.close()
|
|
|
|
$host.ui.RawUI.WindowTitle = $previousTitle
|
2022-11-17 20:02:26 +01:00
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-11-08 12:38:20 +01:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of play-mp3.ps1 as of 11/08/2024 12:37:55)*
|