diff --git a/Wpf_AiSportsMicrospace/Common/Utils.cs b/Wpf_AiSportsMicrospace/Common/Utils.cs index f316c35..1dcaf0d 100644 --- a/Wpf_AiSportsMicrospace/Common/Utils.cs +++ b/Wpf_AiSportsMicrospace/Common/Utils.cs @@ -11,7 +11,8 @@ namespace Wpf_AiSportsMicrospace.Common public static class Utils { private static MediaPlayer _bgPlayer; - public static void PlayBackgroundMusic(string musicFileName) + + public static void PlayBackgroundMusic(string musicFileName, bool loop) { string projectRoot = Path.Combine(AppContext.BaseDirectory, @"..\..\.."); string musicPath = Path.Combine(projectRoot, "Resources", "Music", musicFileName); @@ -20,11 +21,6 @@ namespace Wpf_AiSportsMicrospace.Common { _bgPlayer = new MediaPlayer(); _bgPlayer.Volume = 0.5; - _bgPlayer.MediaEnded += (s, e) => - { - _bgPlayer.Position = TimeSpan.Zero; // 循环播放 - _bgPlayer.Play(); - }; } else { @@ -33,7 +29,25 @@ namespace Wpf_AiSportsMicrospace.Common _bgPlayer.Open(new Uri(musicPath, UriKind.Absolute)); _bgPlayer.Play(); + + if (loop) + { + // 循环播放 → 直接设置 MediaEnded 事件即可 + _bgPlayer.MediaEnded -= BgPlayer_MediaEnded; // 避免重复绑定 + _bgPlayer.MediaEnded += BgPlayer_MediaEnded; + } + else + { + _bgPlayer.MediaEnded -= BgPlayer_MediaEnded; + } } + + private static void BgPlayer_MediaEnded(object sender, EventArgs e) + { + _bgPlayer.Position = TimeSpan.Zero; + _bgPlayer.Play(); + } + public static void StopBackgroundMusic() { if (_bgPlayer != null) diff --git a/Wpf_AiSportsMicrospace/Resources/Music/comeon.mp3 b/Wpf_AiSportsMicrospace/Resources/Music/comeon.mp3 new file mode 100644 index 0000000..4c9af9c Binary files /dev/null and b/Wpf_AiSportsMicrospace/Resources/Music/comeon.mp3 differ diff --git a/Wpf_AiSportsMicrospace/Resources/Music/countdown_3.mp3 b/Wpf_AiSportsMicrospace/Resources/Music/countdown_3.mp3 new file mode 100644 index 0000000..ace3e4f Binary files /dev/null and b/Wpf_AiSportsMicrospace/Resources/Music/countdown_3.mp3 differ diff --git a/Wpf_AiSportsMicrospace/Resources/Music/raisehand.mp3 b/Wpf_AiSportsMicrospace/Resources/Music/raisehand.mp3 new file mode 100644 index 0000000..7d7fc93 Binary files /dev/null and b/Wpf_AiSportsMicrospace/Resources/Music/raisehand.mp3 differ diff --git a/Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs b/Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs index 0be02e2..d24c21e 100644 --- a/Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/CenterHome.xaml.cs @@ -68,7 +68,9 @@ namespace Wpf_AiSportsMicrospace.Views private void Window_Loaded(object sender, RoutedEventArgs e) { _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; - Utils.PlayBackgroundMusic("homeprojectselected.mp3"); + + Utils.PlayBackgroundMusic("homeprojectselected.mp3", true); + } private void CenterHome_Unloaded(object sender, RoutedEventArgs e) diff --git a/Wpf_AiSportsMicrospace/Views/Home.xaml.cs b/Wpf_AiSportsMicrospace/Views/Home.xaml.cs index f5f7cac..f336520 100644 --- a/Wpf_AiSportsMicrospace/Views/Home.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/Home.xaml.cs @@ -34,36 +34,16 @@ namespace Wpf_AiSportsMicrospace /// public partial class Home : UserControl { - private IHumanPredictor _humanPredictor; - private HumanGraphicsRenderer _humanGraphicsRenderer; - private WebcamClient _webcamClient; - private ConcurrentQueue _frameQueue = new(); - private CancellationTokenSource _cts = new(); - private SportOperate _sportOperate; public static Uri loadingImage = new Uri("/Resources/Img/Album/1.gif", UriKind.Relative); - private Main _mainWin => Application.Current.MainWindow as Main; public Home() { InitializeComponent(); - //_humanPredictor = HumanPredictorFactory.Create(HumanPredictorType.SingleHigh); - //_humanGraphicsRenderer = new HumanGraphicsRenderer(); - //_humanGraphicsRenderer.DrawLabel = false; - - //_sports = SportBase.GetSports(); - //_detectQueue = new SportDetectionQueue(); string projectRoot = Path.Combine(AppContext.BaseDirectory, @"..\..\.."); string albumPath = Path.Combine(projectRoot, "Resources", "Img", "Album"); - // 转换为 Uri - //coverFlow.Images.Add(new Uri(Path.Combine(albumPath, "home_play.png"))); - //coverFlow.Images.Add(new Uri(Path.Combine(albumPath, "home_test.png"))); - //coverFlow.Images.Add(new Uri(Path.Combine(albumPath, "home_history.png"))); - //coverFlow.Images.Add(new Uri(Path.Combine(albumPath, "4.jpg"))); - //coverFlow.Images.Add(new Uri(Path.Combine(albumPath, "5.jpg"))); - coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(albumPath, "home_test.png")), ProgressColor1 = "#215bc7", ProgressColor2 = "#fc640e" }); coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(albumPath, "home_play.png")), ProgressColor1 = "#e73d42", ProgressColor2 = "#fd8212" }); coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(albumPath, "home_history.png")), ProgressColor1 = "#e73d42", ProgressColor2 = "#215bc7" }); @@ -73,7 +53,7 @@ namespace Wpf_AiSportsMicrospace Loaded += Window_Loaded; Unloaded += Home_Unloaded; - //AnimationBehavior.SetSourceUri(LoadingImage, loadingImage); + Utils.PlayBackgroundMusic("homeprojectselected.mp3", true); } private void Home_Unloaded(object sender, RoutedEventArgs e) @@ -134,20 +114,6 @@ namespace Wpf_AiSportsMicrospace { Debug.WriteLine($"停止抽帧异常: {ex.Message}"); } - - // 解绑事件,防止重复触发 - //coverFlow.ProgressCompleted -= CoverFlow_ProgressCompleted; - - // 根据图片跳转新窗口 - //string uri = item.ImageUri.ToString(); - - //if (uri.EndsWith("1.jpg")) - // newWindow = new GroupJumpRope(); - //else if (uri.EndsWith("2.jpg")) - // newWindow = new GroupJumpRope(); - //else if (uri.EndsWith("3.jpg")) - // newWindow = new GroupJumpRope(); - // 找到主窗口,切换内容到 GroupJumpRopePage } //测试点击 private void GoNew(object sender, MouseButtonEventArgs e) diff --git a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs index a055d15..39ca8f6 100644 --- a/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/JumpRope/GroupJumpRope.xaml.cs @@ -42,11 +42,6 @@ namespace Wpf_AiSportsMicrospace.Views private List circleTexts = new(); private double[] circlePositionsX = { 0.07, 0.21, 0.36, 0.50, 0.64, 0.78, 0.92 }; private Main _mainWin => Application.Current.MainWindow as Main; - - private DispatcherTimer _countdownTimer; - private int _countdownValue; - private bool _isCountingDown = false; - private SportOperate sportOperate; private List<(double XNorm, double YNorm)> circlePositions = new(); private bool IsGameStarted = false; @@ -56,47 +51,18 @@ namespace Wpf_AiSportsMicrospace.Views Loaded += UserControl_Loaded; Unloaded += UserControl_Unloaded; - _countdownTimer = new DispatcherTimer(); - _countdownTimer.Interval = TimeSpan.FromSeconds(1); - _countdownTimer.Tick += CountdownTimer_Tick; - - sportOperate = new SportOperate(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { DrawCirclesWithText(); _mainWin.HumanFrameUpdated += OnHumanFrameUpdated; + + Utils.PlayBackgroundMusic("raisehand.mp3", false); } private void UserControl_Unloaded(object sender, RoutedEventArgs e) { _mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; } - private void CountdownTimer_Tick(object sender, EventArgs e) - { - if (_countdownValue > 0) - { - countdownText.Text = _countdownValue.ToString(); - _countdownValue--; - } - else - { - _countdownTimer.Stop(); - countdownText.Visibility = Visibility.Collapsed; - _isCountingDown = false; - IsGameStarted = true; // 倒计时结束,游戏正式开始 - } - } - - private void StartCountdown() - { - if (_isCountingDown) return; // 避免重复启动 - - _isCountingDown = true; - _countdownValue = 3; // 从3开始倒计时 - countdownText.Visibility = Visibility.Visible; - _countdownTimer.Start(); - } - private DateTime? _waitStartTime = null; private void OnHumanFrameUpdated(object sender, List humans) { @@ -129,6 +95,7 @@ namespace Wpf_AiSportsMicrospace.Views default: // 没检测到动作,重置倒计时显示 + Utils.StopBackgroundMusic(); countdownText.Text = "3"; countdownText.Visibility = Visibility.Collapsed; break; @@ -153,6 +120,8 @@ namespace Wpf_AiSportsMicrospace.Views countdownText.Text = _currentCountdown.ToString(); countdownText.Visibility = Visibility.Visible; _lastUpdateTime = DateTime.Now; + + Utils.PlayBackgroundMusic("countdown_3.mp3", false); } private void UpdateCountdown() @@ -182,6 +151,8 @@ namespace Wpf_AiSportsMicrospace.Views // 你也可以在这里触发其他动作,例如: // 播放音效、触发事件、执行下一步逻辑 + + Utils.PlayBackgroundMusic("homeprojectselected1.mp3", true); } diff --git a/Wpf_AiSportsMicrospace/Wpf_AiSportsMicrospace.csproj b/Wpf_AiSportsMicrospace/Wpf_AiSportsMicrospace.csproj index ea0c921..fbb23c9 100644 --- a/Wpf_AiSportsMicrospace/Wpf_AiSportsMicrospace.csproj +++ b/Wpf_AiSportsMicrospace/Wpf_AiSportsMicrospace.csproj @@ -48,10 +48,13 @@ + + + @@ -210,6 +213,12 @@ Always + + Always + + + Always + Always @@ -222,6 +231,9 @@ Always + + Always +