-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
play-mp3.ps1
executable file
·53 lines (44 loc) · 1.51 KB
/
play-mp3.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<#
.SYNOPSIS
Plays a .MP3 sound file
.DESCRIPTION
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
.EXAMPLE
PS> ./play-mp3.ps1 C:\thunder.mp3
▶️ Playing 'thunder.mp3' (00:03) ...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$path = "")
try {
if ($path -eq "" ) { $path = Read-Host "Enter the file path to the MP3 sound file" }
if (-not(Test-Path "$path" -pathType leaf)) { throw "Can't access sound file: $path" }
$fullPath = (Get-ChildItem $path).fullname
$filename = (Get-Item "$fullPath").name
Add-Type -assemblyName PresentationCore
$mediaPlayer = New-Object System.Windows.Media.MediaPlayer
do {
$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' ($($minutes.ToString('00')):$($seconds.ToString('00'))) ..."
$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
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}