locked
PowerShell Script to play video with Windows Media Player RRS feed

  • Question

  • Hello Team,

    I have a requirement to run a video via windows Media Player. And also track the duration it play. 

    Suppose I close the video in 5 Sec it should give the duration 5. The below is the script I wrote. But there is problem with this. As the video do not launch nor I am able to the application getting launched . I am only able to here the audio.

    Add-Type -AssemblyName presentationCore
     $filepath = [uri] "C:\Temp\test\Wildlife.wmv"
      $wmplayer = New-Object System.Windows.Media.MediaPlayer
     $wmplayer.Open($filepath)
     Start-Sleep 2 # This allows the $wmplayer time to load the audio file
     $duration = $wmplayer.NaturalDuration.TimeSpan.Seconds
     $wmplayer.Play()
     Start-Sleep $duration
     $wmplayer.Stop()
     $wmplayer.Close()
     Write-Host $duration

    Please help...

    Regards,

    Suman Rout

    • Moved by Bill_Stewart Tuesday, October 11, 2016 7:22 PM User doesn't understand scope of question he is asking
    Wednesday, September 7, 2016 2:05 PM

Answers

  • I did it with the below :

    $timesnow = Get-Date -Format G
        
        Invoke-Item 'C:\Temp\test\abc1.bat'
        
        filter timestamp {"$(Get-Date -Format G): $_"};
            for (;;) {
            do {sleep 1} until (Get-Process vlc 2> $null)
            for (;Get-Process vlc 2> $null ; sleep 1) {};
        
            $timenow = Get-Date -Format G
            
        $s = (New-TimeSpan $timesnow $timenow).TotalSeconds | out-file c:\temp\durationlog.txt
        write-host $s
        exit

    • Marked as answer by Suman Rout Friday, October 28, 2016 1:22 PM
    Friday, October 28, 2016 1:22 PM

All replies

  • Media player control from WPF is not intended to be run outside of a WPF form.


    \_(ツ)_/

    Wednesday, September 7, 2016 2:15 PM
  • I did it with the below :

    $timesnow = Get-Date -Format G
        
        Invoke-Item 'C:\Temp\test\abc1.bat'
        
        filter timestamp {"$(Get-Date -Format G): $_"};
            for (;;) {
            do {sleep 1} until (Get-Process vlc 2> $null)
            for (;Get-Process vlc 2> $null ; sleep 1) {};
        
            $timenow = Get-Date -Format G
            
        $s = (New-TimeSpan $timesnow $timenow).TotalSeconds | out-file c:\temp\durationlog.txt
        write-host $s
        exit

    • Marked as answer by Suman Rout Friday, October 28, 2016 1:22 PM
    Friday, October 28, 2016 1:22 PM