powershell'system.windows.media.mediaplayer'Register-ObjectEvent [英] powershell 'system.windows.media.mediaplayer' Register-ObjectEvent

查看:127
本文介绍了powershell'system.windows.media.mediaplayer'Register-ObjectEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用"system.windows.media.mediaplayer"媒体播放器来播放声音文件列表,但是我无法让媒体播放器知道何时播放下一个声音文件.

I'm trying to use the 'system.windows.media.mediaplayer' media player, to play a list of sound files, however I'm having trouble getting the media player to know when to play the next sound file.

我正在尝试使用"MediaEnded"事件句柄来确定声音文件何时结束,然后移至下一个声音文件.但是我无法正确触发事件句柄,有人可以告诉我我要去哪里了吗?这是我正在使用的代码:

I'm trying to use the 'MediaEnded' event handle to work out when the sound file has ended, then move onto the next sound file. But I can't get the event handle to fire properly, could someone show me where I'm going wrong please? This is the code I'm using:

Add-Type -AssemblyName presentationCore 
$mediaplayer=New-Object system.windows.media.mediaplayer
Register-ObjectEvent -InputObject $mediaplayer -SourceIdentifier media -EventName BufferingStarted -Action {write-host "media stopped"}
$mediaPlayer.Open('C:\Windows\Media\chimes.wav')
$mediaplayer.Play()
sleep 3
$mediaPlayer.Open('C:\Windows\Media\chimes.wav')
$mediaplayer.Play()

我现在正在播放任何声音文件,以尝试触发事件句柄.

I'm just playing any sound file at the moment to try get the event handle to fire.

请帮助我!

推荐答案

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

这篇关于powershell'system.windows.media.mediaplayer'Register-ObjectEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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