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

跟風遠走 提交于 2019-12-04 05:27:35

问题


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.

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.

Please help me !


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


来源:https://stackoverflow.com/questions/17924310/powershell-system-windows-media-mediaplayer-register-objectevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!