【问题标题】:Powershell ISE 5.1 - WindowsMediaPlayer Class - Events not firingPowershell ISE 5.1 - WindowsMediaPlayer 类 - 事件未触发
【发布时间】:2020-10-11 18:44:32
【问题描述】:

有谁知道如何触发 WindowsMediaPlayer 类的 MediaEnded 事件?该事件已注册,但未触发。这不是操作的问题,因为我尝试使用 Out-file 输出结果。我可以使用 Register-ObjectEvent 命令从 Start-Job 触发事件,但由于某种原因,MediaPlayer 不想触发事件。

cls
Unregister-Event *
Add-Type -AssemblyName PresentationCore 
$Player = [System.Windows.Media.MediaPlayer]::new()
$path = "D:\MineSweeper\Sound\Sounds\Bomb.wav"
$file = (Get-Item $path)
$Player.Open($file.FullName)
$player.Play()
$null = Register-ObjectEvent -InputObject $Player -EventName MediaEnded -Action {write-host "$(Get-Date)"}
Get-EventSubscriber

【问题讨论】:

    标签: powershell events windows-media-player


    【解决方案1】:

    这不是真正的 PowerShell 代码或特定问题,如下所述:

    C# MediaPlayer.MediaEnded event not firing.

    但是在这里...

    我正在尝试使用“MediaEnded”事件句柄来解决 声音文件已结束,然后转到下一个声音文件。

    powershell 'system.windows.media.mediaplayer' Register-ObjectEvent

    ... 是一种解决方法。

    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()
    }
    

    【讨论】:

    • 感谢 postanote,但我已经有了这样的解决方法。问题在于将解决方法合并到我的脚本中要复杂得多,以至于我还没有弄清楚如何去做,所以我想看看媒体播放器事件是否可用。我已经在这个问题上工作了一个多月,但仍然没有让我的解决方法或媒体播放器事件起作用。为了让解决方法以某种方式适用于我的情况,我使用 Start-Job 将解决方法代码发送到另一个线程。该作业更新了我的主脚本可以读取的文本文件。文本文件变化时如何触发事件?
    • @postanote,如果您的回答的要点是对另一个问题的回答的逐字复制,请发布包含评论 >链接而不是答案。
    • @user117294,考虑基于FileSystemWatcher 的解决方案 - 例如,请参阅this answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多