From 431d625984214abb8d14a4c9d39a01f593e9eb59 Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Sun, 12 Oct 2025 17:55:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9F=B3=E4=B9=90=E6=92=AD=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Views/JumpRope/GroupJumpRope.xaml.cs | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs index 39ca8f6..751911d 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs @@ -44,6 +44,7 @@ namespace Wpf_AiSportsMicrospace.Views private Main _mainWin => Application.Current.MainWindow as Main; private List<(double XNorm, double YNorm)> circlePositions = new(); + private MediaPlayer _mediaPlayer = new MediaPlayer(); private bool IsGameStarted = false; public GroupJumpRope() { @@ -52,18 +53,49 @@ namespace Wpf_AiSportsMicrospace.Views Unloaded += UserControl_Unloaded; } - private void UserControl_Loaded(object sender, RoutedEventArgs e) + private async void UserControl_Loaded(object sender, RoutedEventArgs e) { DrawCirclesWithText(); - _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; - + // 播放音乐 Utils.PlayBackgroundMusic("raisehand.mp3", false); + + PlayMusic("raisehand.mp3"); } + private void UserControl_Unloaded(object sender, RoutedEventArgs e) { _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; } - private DateTime? _waitStartTime = null; + + private void PlayMusic(string musicFileName) + { + // 获取项目根目录 + string projectRoot = System.IO.Path.Combine(AppContext.BaseDirectory, @"..\..\.."); + string musicPath = System.IO.Path.Combine(projectRoot, "Resources", "Music", musicFileName); + + if (!File.Exists(musicPath)) + { + Console.WriteLine($"音乐文件不存在: {musicPath}"); + return; + } + + _mediaPlayer.Open(new Uri(musicPath, UriKind.Absolute)); + + // 监听播放完成事件 + _mediaPlayer.MediaEnded += MediaPlayer_MediaEnded; + + _mediaPlayer.Play(); + } + + private void MediaPlayer_MediaEnded(object sender, EventArgs e) + { + // 音乐播放完成后的逻辑 + Console.WriteLine("音乐播放完成!"); + + // 可在这里绑定抽帧事件 + _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; + } + private void OnHumanFrameUpdated(object sender, List humans) { try