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

时间:2013-07-29 12:39:40

标签: powershell handle soundplayer

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

我正在尝试使用'MediaEnded'事件句柄来解决声音文件结束时的问题,然后转到下一个声音文件。但我不能让事件处理正常,有人能告诉我哪里出错了吗?这是我正在使用的代码:

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

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

请帮助我!

1 个答案:

答案 0 :(得分:1)

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