From 6970933aee0e4d1b4870fd5a8f3b42c63e241619 Mon Sep 17 00:00:00 2001 From: jasonchenwork Date: Thu, 8 May 2025 11:45:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=9E=E6=AD=8C=E5=8D=A1=E9=A0=93=E7=A7=BB?= =?UTF-8?q?=E5=87=BAdelay?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OverlayFormObj/OverlayForm.cs | 27 +++++++++++++++++++++---- VideoPlayerForm.cs | 38 +++++++++++++++++++++++++---------- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/OverlayFormObj/OverlayForm.cs b/OverlayFormObj/OverlayForm.cs index fc35553..0cf8f7d 100644 --- a/OverlayFormObj/OverlayForm.cs +++ b/OverlayFormObj/OverlayForm.cs @@ -1,5 +1,6 @@ using System.IO; using System.Timers; +using System.Diagnostics; using DBObj; using DualScreenDemo; namespace OverlayFormObj @@ -1512,14 +1513,18 @@ private void DisplayArtists(List artists, int page)//歌星點進去後 ClearDisplay(); DisplaySongs(currentPage); } - + /* 秒數偵測BUG 測試 */ public void AddSongToPlaylist(SongData songData) { + Stopwatch stopwatch = new Stopwatch(); try { + stopwatch.Start(); var filePath1 = songData.SongFilePathHost1; var filePath2 = songData.SongFilePathHost2; - + stopwatch.Stop(); + Console.WriteLine($"[1] 檢查路徑花費: {stopwatch.ElapsedMilliseconds} ms"); + if (!File.Exists(filePath1) && !File.Exists(filePath2)) { // 點播失敗時,寫LOG至logfile.txt @@ -1527,24 +1532,38 @@ private void DisplayArtists(List artists, int page)//歌星點進去後 } else { + stopwatch.Restart(); var pathToPlay = File.Exists(filePath1) ? filePath1 : filePath2; - + stopwatch.Stop(); + Console.WriteLine($"[2] 確定播放路徑花費: {stopwatch.ElapsedMilliseconds} ms"); bool wasEmpty = PrimaryForm.userRequestedSongs.Count == 0; + stopwatch.Restart(); PrimaryForm.userRequestedSongs.Add(songData); PrimaryForm.playedSongsHistory.Add(songData); PrimaryForm.playStates.Add(wasEmpty ? PlayState.Playing : PlayState.NotPlayed); + stopwatch.Stop(); + Console.WriteLine($"[3] 加入播放清單花費: {stopwatch.ElapsedMilliseconds} ms"); // 刷新頁面 + + stopwatch.Restart(); if(PrimaryForm.Instance.multiPagePanel.get_currentSongList() == PrimaryForm.playedSongsHistory) PrimaryForm.Instance.multiPagePanel.LoadSongs(PrimaryForm.Instance.currentSongList); - + stopwatch.Stop(); + Console.WriteLine($"[4] 更新歌曲面板花費: {stopwatch.ElapsedMilliseconds} ms"); if (wasEmpty) { + stopwatch.Restart(); VideoPlayerForm.Instance.SetPlayingSongList(PrimaryForm.userRequestedSongs); PrimaryForm.currentSongIndexInHistory += 1; + stopwatch.Stop(); + Console.WriteLine($"[5] 設定播放列表花費: {stopwatch.ElapsedMilliseconds} ms"); } + stopwatch.Restart(); VideoPlayerForm.Instance.UpdateNextSongFromPlaylist(); PrimaryForm.PrintPlayingSongList(); + stopwatch.Stop(); + Console.WriteLine($"[6] 更新下一首與印出列表花費: {stopwatch.ElapsedMilliseconds} ms"); } } catch (Exception ex) diff --git a/VideoPlayerForm.cs b/VideoPlayerForm.cs index ba27534..d4c2775 100644 --- a/VideoPlayerForm.cs +++ b/VideoPlayerForm.cs @@ -1,6 +1,7 @@ using System.IO; // For StreamWriter using System.Runtime.InteropServices; using DirectShowLib; +using System.Diagnostics; using DBObj; using OverlayFormObj; namespace DualScreenDemo @@ -519,32 +520,48 @@ namespace DualScreenDemo { // [1] 輸出 debug 訊息,方便開發時追蹤是否有呼叫此方法 Console.WriteLine("SetPlayingSongList called"); - + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); // [2] 停止當前播放,釋放資源(例如關閉影片播放器、清除緩衝) StopAndReleaseResources(); - + stopwatch.Stop(); + Console.WriteLine($"[5-2]停止當前播放,釋放資源,耗時{stopwatch.ElapsedMilliseconds}ms"); + // [3] 將新的播放清單指派給 `playingSongList` + stopwatch.Restart(); playingSongList = songList; + stopwatch.Stop(); + Console.WriteLine($"[5-3]將新的播放清單指派,耗時{stopwatch.ElapsedMilliseconds}ms"); // [4] 根據是否有歌,設定旗標為是否播放使用者點播清單 + stopwatch.Restart(); isUserPlaylistPlaying = playingSongList != null && playingSongList.Any(); + stopwatch.Stop(); + Console.WriteLine($"[5-4]根據是否有歌,設定旗標為是否播放使用者點播清單,耗時{stopwatch.ElapsedMilliseconds}ms"); // [5] 強制關閉公播狀態(意即現在進入點歌模式) + stopwatch.Restart(); IsPlayingPublicSong = false; - + stopwatch.Stop(); + Console.WriteLine($"[5-5]強制關閉公播狀態,耗時{stopwatch.ElapsedMilliseconds}ms"); // [6] 若使用者點播清單有歌,就開始播放 if (isUserPlaylistPlaying) - { + { + stopwatch.Restart(); // [6.1] 設定當前歌曲索引為 -1(意味著即將播放第一首,從 `PlayNextSong` 開始) currentSongIndex = -1; - // [6.2] 播放下一首歌(實際會遞增 index 為 0,並播放該首歌) await PlayNextSong(); + stopwatch.Stop(); + Console.WriteLine($"[5-6]播放下一首歌,耗時{stopwatch.ElapsedMilliseconds}ms"); } else { + stopwatch.Restart(); // [7] 如果清單為空,回退播放預設的公播清單(通常是背景播放用) await InitializeAndPlayPublicPlaylist(); + stopwatch.Stop(); + Console.WriteLine($"[5-7]如果清單為空,回退播放預設的公播清單,耗時{stopwatch.ElapsedMilliseconds}ms"); } } @@ -710,10 +727,11 @@ namespace DualScreenDemo // 根據目前播放模式(點歌 or 公播)決定要播放的清單 List currentPlaylist = isUserPlaylistPlaying ? playingSongList : publicPlaylist; - + // 若播放清單是空的,直接返回(不執行播放) if (!currentPlaylist.Any()) return; + if (!isUserPlaylistPlaying) { // 公播模式下,正常循環播放(用 % 保證不會超出陣列界限) @@ -800,6 +818,7 @@ namespace DualScreenDemo private async Task InitializeAndPlayMedia(string pathToPlay) { + if (videoWindowPrimary != null) { videoWindowPrimary.put_Visible(OABool.False); @@ -819,7 +838,6 @@ namespace DualScreenDemo // 渲染媒體文件 RenderMediaFilePrimary(pathToPlay); RenderMediaFileSecondary(pathToPlay); - // 綁定視頻窗口到副屏幕 videoWindowSecondary = (IVideoWindow)videoRendererSecondary; if (videoWindowSecondary != null) @@ -830,13 +848,11 @@ namespace DualScreenDemo await Task.Delay(100); // 給予視窗一些時間進行設置 videoWindowSecondary.put_Visible(OABool.True); } - // 音量處理 if (isMuted) { SetVolume(-10000); } - // 開始播放 if (mediaControlPrimary != null) mediaControlPrimary.Run(); if (mediaControlSecondary != null) mediaControlSecondary.Run(); @@ -845,7 +861,7 @@ namespace DualScreenDemo { SyncToPrimaryMonitor(); } - } + } /// /// 跳至下一首歌曲的方法,會根據目前播放清單(使用者清單或公播清單)做切換與播放邏輯控制。 @@ -1119,7 +1135,7 @@ namespace DualScreenDemo videoWindowPrimary.put_Owner(PrimaryForm.Instance.primaryScreenPanel.Handle); // 设置为 primaryScreenPanel 的句柄 videoWindowPrimary.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipChildren); videoWindowPrimary.SetWindowPosition(0, 0, 1500, 1000); // 调整视频窗口大小以填满黑色区域 - Task.Delay(100).Wait(); + //Task.Delay(100).Wait(); videoWindowPrimary.put_Visible(OABool.True); SaveGraphFile(graphBuilderPrimary, "primary_graph.grf");