# SMTC full info query script (must run in Windows PowerShell 5.1) # Output: EVERY session (not only GetCurrentSession), full media properties Add-Type -AssemblyName System.Runtime.WindowsRuntime -ErrorAction SilentlyContinue $null = [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager, Windows.Media.Control, ContentType=WindowsRuntime] $null = [Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties, Windows.Media.Control, ContentType=WindowsRuntime] $null = [Windows.Storage.Streams.IInputStream, Windows.Storage.Streams, ContentType=WindowsRuntime] function Await($WinRtTask, $ResultType) { $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0] $asTaskGeneric = $asTask.MakeGenericMethod($ResultType) $netTask = $asTaskGeneric.Invoke($null, @($WinRtTask)) $netTask.Wait(-1) | Out-Null $netTask.Result } $mgr = Await ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager]::RequestAsync()) ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionManager]) $current = $mgr.GetCurrentSession() # IVectorView 用 Size/GetAt 枚举:foreach 在 PS 5.1 上经常拿不到元素 $sessions = New-Object System.Collections.ArrayList try { $raw = $mgr.GetSessions() if ($null -ne $raw) { $n = $raw.Size for ($i = 0; $i -lt $n; $i++) { [void]$sessions.Add($raw.GetAt($i)) } } } catch { Write-Host "GetSessions failed: $_" -ForegroundColor Red } Write-Host "===== SMTC Sessions ($($sessions.Count)) =====" -ForegroundColor Cyan $currentId = if ($current) { $current.SourceAppUserModelId } else { '' } Write-Host "CurrentSession : $(if ($currentId) { $currentId } else { '(none)' })" -ForegroundColor Yellow Write-Host "" if ($sessions.Count -eq 0) { if ($current) { [void]$sessions.Add($current) } else { Write-Host "No SMTC media sessions found." -ForegroundColor Yellow return } } $index = 0 foreach ($session in $sessions) { $index++ $appId = $session.SourceAppUserModelId $isCurrent = $currentId -and ($appId -eq $currentId) $mark = if ($isCurrent) { " [CURRENT]" } else { "" } $props = $null try { $props = Await ($session.TryGetMediaPropertiesAsync()) ([Windows.Media.Control.GlobalSystemMediaTransportControlsSessionMediaProperties]) } catch { $props = $null } $playback = $session.GetPlaybackInfo() $timeline = $session.GetTimelineProperties() $position = $timeline.Position $duration = $timeline.EndTime - $timeline.StartTime $progress = if ($duration.TotalSeconds -gt 0) { [math]::Round(($position.TotalSeconds / $duration.TotalSeconds) * 100, 1) } else { 0 } Write-Host ("----- Session #{0}{1} -----" -f $index, $mark) -ForegroundColor Cyan Write-Host "App ID : $appId" if ($props) { Write-Host "Playback Type : $($props.PlaybackType)" Write-Host "Title : $($props.Title)" Write-Host "Subtitle : $($props.Subtitle)" Write-Host "Artist : $($props.Artist)" Write-Host "AlbumArtist : $($props.AlbumArtist)" Write-Host "AlbumTitle : $($props.AlbumTitle)" Write-Host "TrackNumber : $($props.TrackNumber)" Write-Host "Genres : $($props.Genres -join ', ')" $thumbnail = $props.Thumbnail if ($thumbnail) { Write-Host "Thumbnail : (present)" } else { Write-Host "Thumbnail : No thumbnail" } } else { Write-Host "MediaProperties: (failed to read)" } Write-Host "Playback Status: $($playback.PlaybackStatus)" Write-Host "Progress : $position / $duration ($progress%)" Write-Host "" } Write-Host "===================================" -ForegroundColor Cyan