This commit is contained in:
tanglong 2025-10-12 17:21:15 +08:00
parent 0c81072994
commit 6dacaf1494
8 changed files with 43 additions and 78 deletions

View File

@ -11,7 +11,8 @@ namespace Wpf_AiSportsMicrospace.Common
public static class Utils public static class Utils
{ {
private static MediaPlayer _bgPlayer; 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 projectRoot = Path.Combine(AppContext.BaseDirectory, @"..\..\..");
string musicPath = Path.Combine(projectRoot, "Resources", "Music", musicFileName); string musicPath = Path.Combine(projectRoot, "Resources", "Music", musicFileName);
@ -20,11 +21,6 @@ namespace Wpf_AiSportsMicrospace.Common
{ {
_bgPlayer = new MediaPlayer(); _bgPlayer = new MediaPlayer();
_bgPlayer.Volume = 0.5; _bgPlayer.Volume = 0.5;
_bgPlayer.MediaEnded += (s, e) =>
{
_bgPlayer.Position = TimeSpan.Zero; // 循环播放
_bgPlayer.Play();
};
} }
else else
{ {
@ -33,7 +29,25 @@ namespace Wpf_AiSportsMicrospace.Common
_bgPlayer.Open(new Uri(musicPath, UriKind.Absolute)); _bgPlayer.Open(new Uri(musicPath, UriKind.Absolute));
_bgPlayer.Play(); _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() public static void StopBackgroundMusic()
{ {
if (_bgPlayer != null) if (_bgPlayer != null)

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -68,7 +68,9 @@ namespace Wpf_AiSportsMicrospace.Views
private void Window_Loaded(object sender, RoutedEventArgs e) private void Window_Loaded(object sender, RoutedEventArgs e)
{ {
_mainWin.HumanFrameUpdated += OnHumanFrameUpdated; _mainWin.HumanFrameUpdated += OnHumanFrameUpdated;
Utils.PlayBackgroundMusic("homeprojectselected.mp3");
Utils.PlayBackgroundMusic("homeprojectselected.mp3", true);
} }
private void CenterHome_Unloaded(object sender, RoutedEventArgs e) private void CenterHome_Unloaded(object sender, RoutedEventArgs e)

View File

@ -34,36 +34,16 @@ namespace Wpf_AiSportsMicrospace
/// </summary> /// </summary>
public partial class Home : UserControl public partial class Home : UserControl
{ {
private IHumanPredictor _humanPredictor;
private HumanGraphicsRenderer _humanGraphicsRenderer;
private WebcamClient _webcamClient;
private ConcurrentQueue<VideoFrame> _frameQueue = new();
private CancellationTokenSource _cts = new();
private SportOperate _sportOperate;
public static Uri loadingImage = new Uri("/Resources/Img/Album/1.gif", UriKind.Relative); public static Uri loadingImage = new Uri("/Resources/Img/Album/1.gif", UriKind.Relative);
private Main _mainWin => Application.Current.MainWindow as Main; private Main _mainWin => Application.Current.MainWindow as Main;
public Home() public Home()
{ {
InitializeComponent(); 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 projectRoot = Path.Combine(AppContext.BaseDirectory, @"..\..\..");
string albumPath = Path.Combine(projectRoot, "Resources", "Img", "Album"); 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_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_play.png")), ProgressColor1 = "#e73d42", ProgressColor2 = "#fd8212" });
coverFlow.Images.Add(new CoverFlowItem { ImageUri = new Uri(Path.Combine(albumPath, "home_history.png")), ProgressColor1 = "#e73d42", ProgressColor2 = "#215bc7" }); 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; Loaded += Window_Loaded;
Unloaded += Home_Unloaded; Unloaded += Home_Unloaded;
//AnimationBehavior.SetSourceUri(LoadingImage, loadingImage); Utils.PlayBackgroundMusic("homeprojectselected.mp3", true);
} }
private void Home_Unloaded(object sender, RoutedEventArgs e) private void Home_Unloaded(object sender, RoutedEventArgs e)
@ -134,20 +114,6 @@ namespace Wpf_AiSportsMicrospace
{ {
Debug.WriteLine($"停止抽帧异常: {ex.Message}"); 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) private void GoNew(object sender, MouseButtonEventArgs e)

View File

@ -42,11 +42,6 @@ namespace Wpf_AiSportsMicrospace.Views
private List<TextBlock> circleTexts = new(); private List<TextBlock> circleTexts = new();
private double[] circlePositionsX = { 0.07, 0.21, 0.36, 0.50, 0.64, 0.78, 0.92 }; 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 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 List<(double XNorm, double YNorm)> circlePositions = new();
private bool IsGameStarted = false; private bool IsGameStarted = false;
@ -56,47 +51,18 @@ namespace Wpf_AiSportsMicrospace.Views
Loaded += UserControl_Loaded; Loaded += UserControl_Loaded;
Unloaded += UserControl_Unloaded; 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) private void UserControl_Loaded(object sender, RoutedEventArgs e)
{ {
DrawCirclesWithText(); DrawCirclesWithText();
_mainWin.HumanFrameUpdated += OnHumanFrameUpdated; _mainWin.HumanFrameUpdated += OnHumanFrameUpdated;
Utils.PlayBackgroundMusic("raisehand.mp3", false);
} }
private void UserControl_Unloaded(object sender, RoutedEventArgs e) private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{ {
_mainWin.HumanFrameUpdated -= OnHumanFrameUpdated; _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 DateTime? _waitStartTime = null;
private void OnHumanFrameUpdated(object sender, List<Human> humans) private void OnHumanFrameUpdated(object sender, List<Human> humans)
{ {
@ -129,6 +95,7 @@ namespace Wpf_AiSportsMicrospace.Views
default: default:
// 没检测到动作,重置倒计时显示 // 没检测到动作,重置倒计时显示
Utils.StopBackgroundMusic();
countdownText.Text = "3"; countdownText.Text = "3";
countdownText.Visibility = Visibility.Collapsed; countdownText.Visibility = Visibility.Collapsed;
break; break;
@ -153,6 +120,8 @@ namespace Wpf_AiSportsMicrospace.Views
countdownText.Text = _currentCountdown.ToString(); countdownText.Text = _currentCountdown.ToString();
countdownText.Visibility = Visibility.Visible; countdownText.Visibility = Visibility.Visible;
_lastUpdateTime = DateTime.Now; _lastUpdateTime = DateTime.Now;
Utils.PlayBackgroundMusic("countdown_3.mp3", false);
} }
private void UpdateCountdown() private void UpdateCountdown()
@ -182,6 +151,8 @@ namespace Wpf_AiSportsMicrospace.Views
// 你也可以在这里触发其他动作,例如: // 你也可以在这里触发其他动作,例如:
// 播放音效、触发事件、执行下一步逻辑 // 播放音效、触发事件、执行下一步逻辑
Utils.PlayBackgroundMusic("homeprojectselected1.mp3", true);
} }

View File

@ -48,10 +48,13 @@
<None Remove="Resources\Img\test_img\test_jump.png" /> <None Remove="Resources\Img\test_img\test_jump.png" />
<None Remove="Resources\Img\test_img\test_rope.png" /> <None Remove="Resources\Img\test_img\test_rope.png" />
<None Remove="Resources\Img\test_img\test_situp.png" /> <None Remove="Resources\Img\test_img\test_situp.png" />
<None Remove="Resources\Music\comeon.mp3" />
<None Remove="Resources\Music\countdown_3.mp3" />
<None Remove="Resources\Music\homeprojectselected.mp3" /> <None Remove="Resources\Music\homeprojectselected.mp3" />
<None Remove="Resources\Music\homeprojectselected1.mp3" /> <None Remove="Resources\Music\homeprojectselected1.mp3" />
<None Remove="Resources\Music\musicjumprope.mp3" /> <None Remove="Resources\Music\musicjumprope.mp3" />
<None Remove="Resources\Music\musicjumprope1.mp3" /> <None Remove="Resources\Music\musicjumprope1.mp3" />
<None Remove="Resources\Music\raisehand.mp3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -210,6 +213,12 @@
<Resource Include="Resources\Img\test_img\test_situp.png"> <Resource Include="Resources\Img\test_img\test_situp.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource> </Resource>
<Resource Include="Resources\Music\comeon.mp3">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="Resources\Music\countdown_3.mp3">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="Resources\Music\homeprojectselected.mp3"> <Resource Include="Resources\Music\homeprojectselected.mp3">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource> </Resource>
@ -222,6 +231,9 @@
<Resource Include="Resources\Music\musicjumprope1.mp3"> <Resource Include="Resources\Music\musicjumprope1.mp3">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource> </Resource>
<Resource Include="Resources\Music\raisehand.mp3">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup> </ItemGroup>
</Project> </Project>