音乐播放

This commit is contained in:
tanglong 2025-10-12 17:55:34 +08:00
parent 6dacaf1494
commit 431d625984

View File

@ -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<Human> humans)
{
try