使用Windows Media Player播放视频的PowerShell脚本 [英] PowerShell Script to play video with Windows Media Player

查看:686
本文介绍了使用Windows Media Player播放视频的PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Windows Media Player运行视频.并跟踪其播放的持续时间. 假设我在5秒内关闭了视频,视频的持续时间应该为5.以下是我编写的脚本.但这是有问题的.由于视频无法启动,因此我也无法启动该应用程序.我只能在这里音频.

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

请帮助... 问候, 苏曼·卢特(Suman Rout)

解决方案

您需要先创建一个显示的表单,然后创建VideoDrawing,DrawingBrush,然后将其用作表单某些部分的背景.据我了解,MediaElement更易于使用-但是,无论您是否不在此处启动媒体播放器,您都在使用Windows Media对象而未创建在其上显示它们的表单.

如果您只是想打开视频然后关闭,请尝试启动Windows Media Player应用程序.我使用了您的代码,并做了一些您可能想要的事情:

Add-Type -AssemblyName presentationCore
$filepath = "C:\Temp\test\Wildlife.wmv"

#Here we use your code to get the duration of the video
$wmplayer = New-Object System.Windows.Media.MediaPlayer
$wmplayer.Open($filepath)
Start-Sleep 2 
$duration = $wmplayer.NaturalDuration.TimeSpan.Seconds
$wmplayer.Close()

#Here we just open media player and play the file, with an extra second for it to start playing
$proc = Start-process -FilePath wmplayer.exe -ArgumentList $filepath -PassThru
Start-Sleep ($duration + 1)

#Here we kill the media player
Stop-Process $proc.Id -force

Write-Host $duration

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

解决方案

You need to be creating a form that shows up, then creating a VideoDrawing, then a DrawingBrush, and then applying it as the background of some portion of the form. From my understanding, MediaElement is easier to use - but regardless you're not starting media player here, you're using Windows Media objects without creating a form to display them on.

If you merely mean to open the video and close it, try launching the Windows Media Player application instead. I used your code and did something like maybe you're intending:

Add-Type -AssemblyName presentationCore
$filepath = "C:\Temp\test\Wildlife.wmv"

#Here we use your code to get the duration of the video
$wmplayer = New-Object System.Windows.Media.MediaPlayer
$wmplayer.Open($filepath)
Start-Sleep 2 
$duration = $wmplayer.NaturalDuration.TimeSpan.Seconds
$wmplayer.Close()

#Here we just open media player and play the file, with an extra second for it to start playing
$proc = Start-process -FilePath wmplayer.exe -ArgumentList $filepath -PassThru
Start-Sleep ($duration + 1)

#Here we kill the media player
Stop-Process $proc.Id -force

Write-Host $duration

这篇关于使用Windows Media Player播放视频的PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆