mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-25 19:38:17 +02:00
Enhance play-mp3.ps1 and play-m3u.ps1
This commit is contained in:
parent
cd34307547
commit
9bd33214b1
@ -9,6 +9,7 @@ param($Filename = "")
|
|||||||
if ($Filename -eq "" ) { $Filename = read-host "Enter the M3U playlist filename" }
|
if ($Filename -eq "" ) { $Filename = read-host "Enter the M3U playlist filename" }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (-not(test-path "$Filename" -pathType leaf)) { throw "Can't access playlist file: $Filename" }
|
||||||
$Lines = get-content $Filename
|
$Lines = get-content $Filename
|
||||||
|
|
||||||
add-type -assemblyName presentationCore
|
add-type -assemblyName presentationCore
|
||||||
@ -16,22 +17,23 @@ try {
|
|||||||
|
|
||||||
for ([int]$i=0; $i -lt $Lines.Count; $i++) {
|
for ([int]$i=0; $i -lt $Lines.Count; $i++) {
|
||||||
$Line = $Lines[$i]
|
$Line = $Lines[$i]
|
||||||
if ($Line[0] -ne "#") {
|
if ($Line[0] -eq "#") { continue }
|
||||||
|
if (-not(test-path "$Line" -pathType leaf)) { throw "Can't access audio file: $Line" }
|
||||||
$FullPath = (get-childItem "$Line").fullname
|
$FullPath = (get-childItem "$Line").fullname
|
||||||
|
$Filename = (get-item "$FullPath").name
|
||||||
do {
|
do {
|
||||||
$MediaPlayer.open($FullPath)
|
$MediaPlayer.open("$FullPath")
|
||||||
$Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
|
$Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
|
||||||
} until ($Milliseconds)
|
} until ($Milliseconds)
|
||||||
[int]$Minutes = $Milliseconds / 60000
|
[int]$Minutes = $Milliseconds / 60000
|
||||||
[int]$Seconds = ($Milliseconds / 1000) % 60
|
[int]$Seconds = ($Milliseconds / 1000) % 60
|
||||||
"▶️Playing 🎵$Line ($($Minutes):$Seconds) ..."
|
"▶️Playing 🎵$Filename ($($Minutes.ToString('00')):$($Seconds.ToString('00'))) ..."
|
||||||
$MediaPlayer.Volume = 1
|
$MediaPlayer.Volume = 1
|
||||||
$MediaPlayer.play()
|
$MediaPlayer.play()
|
||||||
start-sleep -milliseconds $Milliseconds
|
start-sleep -milliseconds $Milliseconds
|
||||||
$MediaPlayer.stop()
|
$MediaPlayer.stop()
|
||||||
$MediaPlayer.close()
|
$MediaPlayer.close()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
exit 0
|
exit 0
|
||||||
} catch {
|
} catch {
|
||||||
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
@ -9,19 +9,21 @@ param($Filename = "")
|
|||||||
if ($Filename -eq "" ) { $Filename = read-host "Enter the MP3 filename" }
|
if ($Filename -eq "" ) { $Filename = read-host "Enter the MP3 filename" }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$Filename = resolve-path -path "$Filename" -relative
|
if (-not(test-path "$Filename" -pathType leaf)) { throw "Can't access audio file: $Filename" }
|
||||||
|
$FullPath = (get-childItem $Filename).fullname
|
||||||
|
$Filename = (get-item "$FullPath").name
|
||||||
|
|
||||||
add-type -assemblyName PresentationCore
|
add-type -assemblyName PresentationCore
|
||||||
$MediaPlayer = new-object System.Windows.Media.MediaPlayer
|
$MediaPlayer = new-object System.Windows.Media.MediaPlayer
|
||||||
|
|
||||||
$FullPath = (get-childItem $Filename).fullname
|
|
||||||
do {
|
do {
|
||||||
$MediaPlayer.open($FullPath)
|
$MediaPlayer.open($FullPath)
|
||||||
$Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
|
$Milliseconds = $MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
|
||||||
} until ($Milliseconds)
|
} until ($Milliseconds)
|
||||||
|
|
||||||
[int]$Minutes = $Milliseconds / 60000
|
[int]$Minutes = $Milliseconds / 60000
|
||||||
[int]$Seconds = ($Milliseconds / 1000) % 60
|
[int]$Seconds = ($Milliseconds / 1000) % 60
|
||||||
"▶️Playing 🎵$Filename ($($Minutes):$Seconds) ..."
|
"▶️Playing 🎵$Filename ($($Minutes.ToString('00')):$($Seconds.ToString('00'))) ..."
|
||||||
$MediaPlayer.Volume = 1
|
$MediaPlayer.Volume = 1
|
||||||
$MediaPlayer.play()
|
$MediaPlayer.play()
|
||||||
start-sleep -milliseconds $Milliseconds
|
start-sleep -milliseconds $Milliseconds
|
||||||
|
Loading…
Reference in New Issue
Block a user