Updated play-m3u.ps1 and play-mp3.ps1

This commit is contained in:
Markus Fleschutz 2024-11-21 08:07:03 +01:00
parent c54e4b0184
commit de784e4cdc
2 changed files with 19 additions and 27 deletions

View File

@ -1,12 +1,14 @@
<#
.SYNOPSIS
Plays a playlist (.M3U format)
Plays a .M3U playlist
.DESCRIPTION
This PowerShell script plays the given playlist (in .M3U file format)
.PARAMETER filename
Specifies the path to the playlist
.EXAMPLE
PS> ./play-m3u C:\MyPlaylist.m3u
PS> ./play-m3u.ps1 C:\MyPlaylist.m3u
Playing '01 Sandy beach - strong waves.mp3' (02:54) ...
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
@ -16,32 +18,20 @@
param([string]$filename = "")
try {
if ($filename -eq "" ) { $filename = read-host "Enter the M3U playlist filename" }
if ($filename -eq "" ) { $filename = Read-Host "Enter the path to the .M3U playlist file" }
if (-not(test-path "$filename" -pathType leaf)) { throw "Can't access playlist file: $filename" }
$Lines = get-content $filename
if (-not(Test-Path "$filename" -pathType leaf)) { throw "Can't access playlist file: $filename" }
$lines = Get-Content $filename
add-type -assemblyName presentationCore
$MediaPlayer = new-object system.windows.media.mediaplayer
Add-Type -assemblyName presentationCore
$MediaPlayer = New-Object system.windows.media.mediaplayer
for ([int]$i=0; $i -lt $Lines.Count; $i++) {
$Line = $Lines[$i]
if ($Line[0] -eq "#") { continue }
if (-not(test-path "$Line" -pathType leaf)) { throw "Can't access audio file: $Line" }
$FullPath = (get-childItem "$Line").fullname
$filename = (get-item "$FullPath").name
do {
$MediaPlayer.open("$FullPath")
$Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
} until ($Milliseconds)
[int]$Minutes = $Milliseconds / 60000
[int]$Seconds = ($Milliseconds / 1000) % 60
"Playing 🎵$filename ($($Minutes.ToString('00')):$($Seconds.ToString('00'))) ..."
$MediaPlayer.Volume = 1
$MediaPlayer.play()
start-sleep -milliseconds $Milliseconds
$MediaPlayer.stop()
$MediaPlayer.close()
foreach ($line in $lines) {
if ($line[0] -eq "#") { continue }
if (-not(Test-Path "$line" -pathType leaf)) { throw "Can't access audio file: $line" }
$fullPath = (Get-ChildItem "$line").fullname
& "$PSScriptRoot/play-mp3.ps1" $fullPath
}
exit 0 # success
} catch {

View File

@ -7,6 +7,7 @@
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
@ -33,16 +34,17 @@ try {
[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..."
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 {