Files
2026-09-28 00:21:12 +08:00

39 lines
1.9 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! GSMTC 可用性探测(MUSIC_REMOTE_PLAN §7 P0 第 1 项 / §6 第 1 条)
//!
//! 关键假设:GSMTC(Windows.Media.Control)在**非打包**桌面应用可用(Windows 10 1809+)。
//! 本 example 以普通控制台进程运行(无 package identity),跑通即证明假设成立。
//!
//! 运行:`cargo run --example gsmtc_probe`
//! 建议先在任意播放器(网易云/浏览器/Thing)放一首歌,可看到当前会话的元数据与时间轴。
use windows::Media::Control::GlobalSystemMediaTransportControlsSessionManager;
use windows::core::Result;
fn main() -> Result<()> {
// 1. 取全局会话管理器(失败 = 整个能力需降级,见计划 §3.1 失败策略)
let mgr = GlobalSystemMediaTransportControlsSessionManager::RequestAsync()?.get()?;
// 2. 枚举系统全部 SMTC 会话(注意:0.52 投影里 GetSessions/GetCurrentSession 是同步的)
let sessions = mgr.GetSessions()?;
println!("[ok] GSMTC 可用(unpackaged),当前会话数: {}", sessions.Size()?);
// 3. 当前活跃会话的元数据 / 播放状态 / 时间轴
// 无会话时 ABI 返回 null → windows-rs 投影为 Err(E_POINTER),按"无会话"处理
match mgr.GetCurrentSession() {
Ok(cur) => {
println!("current: {}", cur.SourceAppUserModelId()?);
let p = cur.TryGetMediaPropertiesAsync()?.get()?;
println!(" {} - {} | album: {}", p.Artist()?, p.Title()?, p.AlbumTitle()?);
println!(" status: {:?}", cur.GetPlaybackInfo()?.PlaybackStatus()?);
let tl = cur.GetTimelineProperties()?;
println!(
" position(ticks): {} | lastUpdated(ticks): {}",
tl.Position()?.Duration,
tl.LastUpdatedTime()?.UniversalTime
);
}
Err(e) => println!("current session: <none> ({e})(先放一首歌再跑,可看完整元数据)"),
}
Ok(())
}