简体   繁体   中英

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

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 !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM