音乐
This commit is contained in:
parent
0c81072994
commit
6dacaf1494
@ -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)
|
||||
|
||||
BIN
Wpf_AiSportsMicrospace/Resources/Music/comeon.mp3
Normal file
BIN
Wpf_AiSportsMicrospace/Resources/Music/comeon.mp3
Normal file
Binary file not shown.
BIN
Wpf_AiSportsMicrospace/Resources/Music/countdown_3.mp3
Normal file
BIN
Wpf_AiSportsMicrospace/Resources/Music/countdown_3.mp3
Normal file
Binary file not shown.
BIN
Wpf_AiSportsMicrospace/Resources/Music/raisehand.mp3
Normal file
BIN
Wpf_AiSportsMicrospace/Resources/Music/raisehand.mp3
Normal file
Binary file not shown.
@ -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)
|
||||
|
||||
@ -34,36 +34,16 @@ namespace Wpf_AiSportsMicrospace
|
||||
/// </summary>
|
||||
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);
|
||||
|
||||
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)
|
||||
|
||||
@ -42,11 +42,6 @@ namespace Wpf_AiSportsMicrospace.Views
|
||||
private List<TextBlock> 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<Human> 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -48,10 +48,13 @@
|
||||
<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_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\homeprojectselected1.mp3" />
|
||||
<None Remove="Resources\Music\musicjumprope.mp3" />
|
||||
<None Remove="Resources\Music\musicjumprope1.mp3" />
|
||||
<None Remove="Resources\Music\raisehand.mp3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -210,6 +213,12 @@
|
||||
<Resource Include="Resources\Img\test_img\test_situp.png">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</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">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Resource>
|
||||
@ -222,6 +231,9 @@
|
||||
<Resource Include="Resources\Music\musicjumprope1.mp3">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Resource>
|
||||
<Resource Include="Resources\Music\raisehand.mp3">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Resource>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user