-1

I have reference of : powershell 'system.windows.media.mediaplayer' Register-ObjectEvent, for handling audio files, using PowerShell. It shows code, for playing audio files, in a folder. But doesn't explains further or comment on its using.
What I want is PowerShell, to create a Windows Form, with select files dialog, basic icons, for play/pause/stop, and displaying its current time status,( how many minutes passed, remaining, on seek bar).

Here is code for reference:

Add-Type -AssemblyName PresentationCore 
$_MediaPlayer = New-Object System.Windows.Media.MediaPlayer 
$_MusicFolder = 'C:\Users\Making JESUS Proud\Music'
$_MusicFiles = Get-ChildItem -path $_MusicFolder -include *.mp3 -recurse
$duration = $null
foreach($_file in $_MusicFiles){ 
     "Playing $($_file.BaseName)"
     [uri]$_song = $_file.FullName
     do {
        $_MediaPlayer.Open($_song)
        $_songDuration = $_MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
     }
     until ($_songDuration)
     $_MediaPlayer.Volume = 1
     $_MediaPlayer.Play()
     Start-Sleep -Milliseconds $_songDuration
     $_MediaPlayer.Stop()
     $_MediaPlayer.Close()
}

I am ready with GUI part, but I am not able to pull information from MediaPlayer class, regarding its timeline progress, and event handling to pause/stop/replay audio.

Documentation for class:

https://msdn.microsoft.com/en-us/library/System.Windows.Media.MediaPlayer%28v=vs.110%29.aspx

About time line related I have found, three following properties, but I don't understand them, properly.

MediaPlayer.NaturalDuration   
MediaPlayer.Clock  
MediaPlayer.Position  
Community
  • 1
  • 1
msinfo
  • 1,147
  • 6
  • 21
  • 39

1 Answers1

1
Add-Type –assemblyName PresentationFramework
$mp=New-Object System.Windows.Controls.MediaElement
$mp.Source='E:\onedrive\audio\song.mp3'
$mp.UnloadedBehavior='Manual'
$mp.Position=New-Object System.TimeSpan(0, 0, 0, 30, 0)
$mp.Volume = 1
$mp.Play()

Note:
You can only set the position on a file before the file is opened.
You can seek not all file types, but most of all.

Reference:
https://msdn.microsoft.com/en-us/library/ms748248%28v=vs.110%29.aspx

msinfo
  • 1,147
  • 6
  • 21
  • 39