【问题标题】:PowerShell Script to play video with Windows Media Player使用 Windows Media Player 播放视频的 PowerShell 脚本
【发布时间】:2016-09-08 10:50:00
【问题描述】:

我需要通过 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

请帮忙... 问候, 苏曼溃败

【问题讨论】:

    标签: powershell media-player


    【解决方案1】:

    您需要创建一个显示的表单,然后创建一个 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
    

    【讨论】:

    • 嗨,克里斯,谢谢。但是当我在两者之间关闭播放器时,我得到以下错误和持续时间为 30,这是视频的完整持续时间,而不是视频运行的持续时间......Stop-Process:找不到带有进程标识符 7828。在 line:16 char:1 + Stop-Process $proc.Id -force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (7828:Int32) [Stop-Process], ProcessCommandException + FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.StopProcessCommand 30
    • 是的 - 所以如果你想抓住它,用 Get-Process 检查有那个 pid 的东西,如果没有,不要停止它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多