我正在尝试使用"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:WindowsMediachimes.wav')
$mediaplayer.Play()
sleep 3
$mediaPlayer.Open('C:WindowsMediachimes.wav')
$mediaplayer.Play()
我现在只是在播放任何声音文件,试图启动事件句柄。
请帮帮我!
Add-Type -AssemblyName PresentationCore
$_MediaPlayer = New-Object System.Windows.Media.MediaPlayer
$_MusicFolder = 'C:UsersMaking JESUS ProudMusic'
$_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()
}