點歌卡頓移出delay

This commit is contained in:
jasonchenwork 2025-05-08 11:45:47 +08:00
parent d49a99822e
commit 6970933aee
2 changed files with 50 additions and 15 deletions

View File

@ -1,5 +1,6 @@
using System.IO;
using System.Timers;
using System.Diagnostics;
using DBObj;
using DualScreenDemo;
namespace OverlayFormObj
@ -1512,13 +1513,17 @@ private void DisplayArtists(List<Artist> 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))
{
@ -1527,24 +1532,38 @@ private void DisplayArtists(List<Artist> 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)

View File

@ -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");
}
}
@ -714,6 +731,7 @@ namespace DualScreenDemo
// 若播放清單是空的,直接返回(不執行播放)
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();
@ -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");