From 7b5fed433286af14307add42506a27d3fc00832c Mon Sep 17 00:00:00 2001 From: jasonchenwork Date: Thu, 10 Jul 2025 15:55:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AA=BF=E6=95=B4=20=E5=85=AC=E6=92=AD?= =?UTF-8?q?=E6=AD=8C=E5=96=AE=20=E9=82=8F=E8=BC=AF=E8=AA=BF=E6=95=B4=20202?= =?UTF-8?q?50710?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CommandHandler.cs | 2 +- OverlayFormObj/OverlayForm.Labels.cs | 5 +- .../PrimaryForm.MultiPagePanel.cs | 2 +- Program.cs | 4 +- TCPServer.cs | 5 +- VideoPlayerForm.cs | 210 +++--------------- superstar.csproj | 2 +- 7 files changed, 39 insertions(+), 191 deletions(-) diff --git a/CommandHandler.cs b/CommandHandler.cs index 1e54917..605f39d 100644 --- a/CommandHandler.cs +++ b/CommandHandler.cs @@ -616,7 +616,7 @@ namespace DualScreenDemo int completedCount = PrimaryForm.currentSongIndexInHistory; // 判斷是否正在播放公播歌單 (若用戶點播歌單為空,則播放公播歌單) bool isPlayingPublicList = PrimaryForm.userRequestedSongs.Count == 0 || - (PrimaryForm.currentSongIndexInHistory >= PrimaryForm.userRequestedSongs.Count - 1 && PrimaryForm.Instance.videoPlayerForm.IsPlayingPublicSong); + (PrimaryForm.currentSongIndexInHistory >= PrimaryForm.userRequestedSongs.Count - 1 ); if (isPlayingPublicList) { for (int i = startIndex; i < endIndex; i++) diff --git a/OverlayFormObj/OverlayForm.Labels.cs b/OverlayFormObj/OverlayForm.Labels.cs index fbc1bdb..e4fc136 100644 --- a/OverlayFormObj/OverlayForm.Labels.cs +++ b/OverlayFormObj/OverlayForm.Labels.cs @@ -601,11 +601,10 @@ namespace OverlayFormObj -public void UpdateNextSongLabelFromPlaylist(bool isUserPlaylistPlaying, SongData currentPlayingSong) +public void UpdateNextSongLabelFromPlaylist( SongData currentPlayingSong) { // 获取播放列表 - List currentPlaylist = isUserPlaylistPlaying ? - VideoPlayerForm.playingSongList : VideoPlayerForm.publicPlaylist; + List currentPlaylist = VideoPlayerForm.playingSongList ; if (currentPlaylist == null || currentPlaylist.Count == 0) { diff --git a/PrimaryFormParts/PrimaryForm.MultiPagePanel.cs b/PrimaryFormParts/PrimaryForm.MultiPagePanel.cs index bece5e3..e764d2d 100644 --- a/PrimaryFormParts/PrimaryForm.MultiPagePanel.cs +++ b/PrimaryFormParts/PrimaryForm.MultiPagePanel.cs @@ -387,7 +387,7 @@ namespace DualScreenDemo { // 判斷是否正在播放公播歌單 (若用戶點播歌單為空,則播放公播歌單) bool isPlayingPublicList = userRequestedSongs.Count == 0 || - (currentSongIndexInHistory >= userRequestedSongs.Count - 1 && PrimaryForm.Instance.videoPlayerForm.IsPlayingPublicSong); + (currentSongIndexInHistory >= userRequestedSongs.Count - 1 ); if (isPlayingPublicList) { // 若播放公播歌單,代表已點歌曲皆已播放完畢 diff --git a/Program.cs b/Program.cs index 3d7787e..a211cfc 100644 --- a/Program.cs +++ b/Program.cs @@ -150,7 +150,7 @@ namespace DualScreenDemo primaryForm.videoPlayerForm.Show(); // 初始化公共播放列表 - primaryForm.videoPlayerForm.InitializePublicPlaylist(cherker.GetSongs()); + primaryForm.videoPlayerForm.PlayPublicPlaylist(); } } } @@ -174,7 +174,7 @@ namespace DualScreenDemo { primaryForm.videoPlayerForm = new VideoPlayerForm(); // primaryForm.primaryMediaPlayerForm = new PrimaryMediaPlayerForm(primaryForm, primaryForm.secondaryMediaPlayerForm); - primaryForm.videoPlayerForm.InitializePublicPlaylist(cherker.GetSongs()); + primaryForm.videoPlayerForm.PlayPublicPlaylist(); primaryForm.videoPlayerForm.Show(); } } diff --git a/TCPServer.cs b/TCPServer.cs index 48ac2ac..6c321c3 100644 --- a/TCPServer.cs +++ b/TCPServer.cs @@ -139,7 +139,6 @@ namespace DualScreenDemo if (File.Exists(closePath)) { SongData closeSong = new SongData("0", "結束播放",closePath, 1,true); - VideoPlayerForm.publicPlaylist = new List(); VideoPlayerForm.playingSongList = new List(); PrimaryForm.playedSongsHistory = new List(); if (VideoPlayerForm.Instance.currentPlayingSong != null) @@ -148,8 +147,7 @@ namespace DualScreenDemo } VideoPlayerForm.playingSongList.Add(closeSong); - VideoPlayerForm.publicPlaylist.Add(closeSong); - + PrimaryForm.userRequestedSongs = new List(); if (IsFormReady(OverlayForm.MainForm)) @@ -189,7 +187,6 @@ namespace DualScreenDemo { // 開台時跳至首頁 - VideoPlayerForm.publicPlaylist = new List(); VideoPlayerForm.playingSongList = new List(); VideoPlayerForm.Instance.PlayPublicPlaylist(); PrimaryForm.currentSongIndexInHistory = -1; diff --git a/VideoPlayerForm.cs b/VideoPlayerForm.cs index 704e459..894eea9 100644 --- a/VideoPlayerForm.cs +++ b/VideoPlayerForm.cs @@ -10,7 +10,6 @@ namespace DualScreenDemo { #region 防止閃屏 public SongData currentPlayingSong; - public bool IsPlayingPublicSong { get; private set; } = false; protected override CreateParams CreateParams { get @@ -93,9 +92,7 @@ namespace DualScreenDemo public static OverlayForm overlayForm; public static List playingSongList; - public static List publicPlaylist; public static int currentSongIndex = 0; - private static bool isUserPlaylistPlaying = false; public bool isMuted = false; public int previousVolume = -1000; public bool isPaused = false; @@ -536,12 +533,6 @@ namespace DualScreenDemo } } - public async void InitializePublicPlaylist(List initialPlaylist) - { - publicPlaylist = initialPlaylist; - await PlayPublicPlaylist(); // 开始播放公播歌单 - } - public async Task SetPlayingSongList(List songList) { // [1] 輸出 debug 訊息,方便開發時追蹤是否有呼叫此方法 @@ -554,26 +545,11 @@ namespace DualScreenDemo // [3] 將新的播放清單指派給 `playingSongList` playingSongList = songList; - - // [4] 根據是否有歌,設定旗標為是否播放使用者點播清單 - isUserPlaylistPlaying = playingSongList != null && playingSongList.Any(); - - - // [5] 強制關閉公播狀態(意即現在進入點歌模式) - IsPlayingPublicSong = false; - // [6] 若使用者點播清單有歌,就開始播放 - if (isUserPlaylistPlaying) - { - // [6.1] 設定當前歌曲索引為 -1(意味著即將播放第一首,從 `PlayNextSong` 開始) - currentSongIndex = -1; - // [6.2] 播放下一首歌(實際會遞增 index 為 0,並播放該首歌) - await PlayNextSong(); - } - else - { - await InitializeAndPlayPublicPlaylist(); - } + // [6.1] 設定當前歌曲索引為 -1(意味著即將播放第一首,從 `PlayNextSong` 開始) + currentSongIndex = -1; + // [6.2] 播放下一首歌(實際會遞增 index 為 0,並播放該首歌) + await PlayNextSong(); } @@ -597,59 +573,9 @@ namespace DualScreenDemo ); } } - - isUserPlaylistPlaying = false; - IsPlayingPublicSong = true; // 设置为正在播放公播 currentSongIndex = -1; - try - { - // 重新整理公播清單 - publicPlaylist = new List(); - - // 首先添加 welcome.mpg - string welcomePath = @"D:\video\welcome.mpg"; - if (File.Exists(welcomePath)) - { - publicPlaylist.Add(new SongData( - "0", "歡迎光臨", welcomePath, 1,true - )); - } - - // 添加 BGM 序列 - for (int i = 1; i <= 99; i++) - { - string bgmPath = $@"D:\video\BGM{i:D2}.mpg"; - if (File.Exists(bgmPath)) - { - publicPlaylist.Add(new SongData( - i.ToString(), $"背景音樂{i:D2}", bgmPath, 1,true - )); - } - } - - // 如果公播清單為空,使用原有的歌曲 - if (publicPlaylist.Count == 0) - { - string videoDirectory = @"D:\video\"; - string[] videoFiles = Directory.GetFiles(videoDirectory, "*.mpg"); - foreach (var songPath in videoFiles) - { - string fileName = Path.GetFileNameWithoutExtension(songPath); - publicPlaylist.Add(new SongData("0", fileName,songPath,1,true)); - } - } - - await Task.Delay(100); // 添加短暂延迟 - await PlayNextSong(); - } - catch (Exception ex) - { - Console.WriteLine($"播放公播清單時發生錯誤: {ex.Message}"); - // 可以在这里添加重试逻辑 - await Task.Delay(1000); - await PlayNextSong(); - } + await PlayNextSong(); } // private static async Task UpdateMarqueeTextForCurrentSong(SongData song) @@ -701,11 +627,11 @@ namespace DualScreenDemo } public void UpdateNextSongFromPlaylist() { - List currentPlaylist = isUserPlaylistPlaying ? playingSongList : publicPlaylist; + List currentPlaylist = playingSongList ; if (currentPlaylist == null || currentPlaylist.Count == 0) { - overlayForm?.UpdateNextSongLabelFromPlaylist(isUserPlaylistPlaying, null); + overlayForm?.UpdateNextSongLabelFromPlaylist( null); return; } @@ -713,12 +639,12 @@ namespace DualScreenDemo if (currentSongIndex == -1 || currentSongIndex + 1 >= currentPlaylist.Count) { - overlayForm?.UpdateNextSongLabelFromPlaylist(isUserPlaylistPlaying, null); + overlayForm?.UpdateNextSongLabelFromPlaylist( null); } else { SongData nextSong = currentPlaylist[currentSongIndex + 1]; - overlayForm?.UpdateNextSongLabelFromPlaylist(isUserPlaylistPlaying, currentPlayingSong); + overlayForm?.UpdateNextSongLabelFromPlaylist( currentPlayingSong); } } @@ -733,30 +659,21 @@ namespace DualScreenDemo Console.WriteLine("開始播放下一首歌曲..."); // 根據目前播放模式(點歌 or 公播)決定要播放的清單 - List currentPlaylist = isUserPlaylistPlaying ? playingSongList : publicPlaylist; + List currentPlaylist = playingSongList ; // 若播放清單是空的,直接返回(不執行播放) if (!currentPlaylist.Any()) return; - - if (!isUserPlaylistPlaying) + // 若是使用者點播模式,先送出升Key的串口指令 + if (SerialPortManager.mySerialPort != null && SerialPortManager.mySerialPort.IsOpen) { - // 公播模式下,正常循環播放(用 % 保證不會超出陣列界限) - currentSongIndex = (currentSongIndex + 1) % currentPlaylist.Count; - Console.WriteLine($"順序播放: currentSongIndex = {currentSongIndex}, currentPlaylist.Count = {currentPlaylist.Count}"); + byte[] commandBytesIncreasePitch1 = new byte[] { 0xA2, 0x7F, 0xA4 }; + SerialPortManager.mySerialPort.Write(commandBytesIncreasePitch1, 0, commandBytesIncreasePitch1.Length); } - else - { - // 若是使用者點播模式,先送出升Key的串口指令 - if (SerialPortManager.mySerialPort != null && SerialPortManager.mySerialPort.IsOpen) - { - byte[] commandBytesIncreasePitch1 = new byte[] { 0xA2, 0x7F, 0xA4 }; - SerialPortManager.mySerialPort.Write(commandBytesIncreasePitch1, 0, commandBytesIncreasePitch1.Length); - } - // 同樣遞增 index 來播放下一首 - currentSongIndex = (currentSongIndex + 1) % currentPlaylist.Count; - } + // 同樣遞增 index 來播放下一首 + currentSongIndex = (currentSongIndex + 1) % currentPlaylist.Count; + // 可以取得 燈控/聲控 的位置 @@ -887,34 +804,18 @@ namespace DualScreenDemo await Task.Delay(100); // 如果目前正在播放使用者點播清單,且清單還有歌曲 - if (isUserPlaylistPlaying && playingSongList != null && playingSongList.Count > 0) + if ( playingSongList != null && playingSongList.Count > 0) { // 移除目前播放的第一首歌(已經播放過的) playingSongList.RemoveAt(0); - if (playingSongList.Count == 0) + if (playingSongList.Count <= 0) { - // 使用者點播清單播完了,切換回公播清單 - Console.WriteLine("用戶播放列表已清空,切換至公播清單"); - isUserPlaylistPlaying = false; lastPlayedIndex = -1; - currentSongIndex = -1; - - // 初始化並開始播放公播清單 - await InitializeAndPlayPublicPlaylist(); - } - else - { - // 還有歌曲可播,繼續播放下一首使用者歌曲 - currentSongIndex = -1; - await PlayNextSong(); } } - else - { - // 如果不是在播放使用者清單(或清單為空),直接初始化並播放公播清單 - await InitializeAndPlayPublicPlaylist(); - } + currentSongIndex = -1; + await PlayNextSong(); // 如果目前歌曲在播放歷史列表的索引是合法的(防呆) if (PrimaryForm.currentSongIndexInHistory >= 0 && PrimaryForm.currentSongIndexInHistory < PrimaryForm.playedSongsHistory.Count) @@ -929,7 +830,7 @@ namespace DualScreenDemo } /*如果當前為公播,不可以+1*/ bool isPlayingPublicList = PrimaryForm.userRequestedSongs.Count == 0 || - (PrimaryForm.currentSongIndexInHistory >= PrimaryForm.userRequestedSongs.Count - 1 && PrimaryForm.Instance.videoPlayerForm.IsPlayingPublicSong); + (PrimaryForm.currentSongIndexInHistory >= PrimaryForm.userRequestedSongs.Count - 1 ); if (!isPlayingPublicList) { PrimaryForm.currentSongIndexInHistory += 1; @@ -940,53 +841,15 @@ namespace DualScreenDemo { // 若有例外錯誤,列印錯誤訊息並切回公播模式 Console.WriteLine($"切換歌曲時發生錯誤: {ex.Message}"); - await InitializeAndPlayPublicPlaylist(); - } - } - - - // 新增一个方法来处理公播列表的初始化和播放 - private async Task InitializeAndPlayPublicPlaylist() - { - try - { - isUserPlaylistPlaying = false; currentSongIndex = -1; - // 重新初始化公播列表 - publicPlaylist = new List(); - - // 添加 welcome.mpg - string welcomePath = @"D:\video\welcome.mpg"; - if (File.Exists(welcomePath)) - { - publicPlaylist.Add(new SongData("0", "歡迎光臨", welcomePath, 1,true)); - } - - // 添加 BGM 序列 - for (int i = 1; i <= 99; i++) - { - string bgmPath = $@"D:\video\BGM{i:D2}.mpg"; - if (File.Exists(bgmPath)) - { - publicPlaylist.Add(new SongData(i.ToString(), $"背景音樂{i:D2}", bgmPath, 1,true)); - } - } - - if (publicPlaylist.Count > 0) - { - await PlayNextSong(); - } - } - catch (Exception ex) - { - Console.WriteLine($"初始化公播清單時發生錯誤: {ex.Message}"); + await PlayNextSong(); } } public void ReplayCurrentSong() { - List currentPlaylist = isUserPlaylistPlaying ? playingSongList : publicPlaylist; + List currentPlaylist = playingSongList ; if (!currentPlaylist.Any()) return; var songToPlay = currentPlaylist[currentSongIndex]; var pathToPlay = songToPlay.getFile(); @@ -1302,7 +1165,7 @@ namespace DualScreenDemo StopAndReleaseResources(); await Task.Delay(100); // 给予足够的时间释放资源 - if (isUserPlaylistPlaying && playingSongList != null) + if ( playingSongList != null) { if (playingSongList.Count > 0) { @@ -1351,7 +1214,6 @@ namespace DualScreenDemo } // 重置播放状态但保留历史记录 - isUserPlaylistPlaying = false; currentSongIndex = -1; // 确保所有未播放的歌曲状态被清除 @@ -1366,7 +1228,9 @@ namespace DualScreenDemo // 添加延迟以确保状态更新完成 await Task.Delay(200); - await InitializeAndPlayPublicPlaylist(); + currentSongIndex = -1; + + await PlayNextSong(); } else { @@ -1378,25 +1242,20 @@ namespace DualScreenDemo { Console.WriteLine($"處理播放列表時發生錯誤: {ex.Message}"); lastPlayedIndex = -1; - isUserPlaylistPlaying = false; currentSongIndex = -1; - await InitializeAndPlayPublicPlaylist(); + + await PlayNextSong(); } } } - else if (!isUserPlaylistPlaying && publicPlaylist != null) - { - await PlayNextSong(); - } } catch (Exception ex) { Console.WriteLine($"切換歌曲時發生錯誤: {ex.Message}"); // 发生错误时重置状态并切换到公播 lastPlayedIndex = -1; - isUserPlaylistPlaying = false; currentSongIndex = -1; - await InitializeAndPlayPublicPlaylist(); + await PlayNextSong(); } finally { @@ -1413,13 +1272,6 @@ namespace DualScreenDemo Console.WriteLine($"監控媒體事件時發生錯誤: {ex.Message}"); isPlayingNext = false; - // 添加重试机制 - if (!isUserPlaylistPlaying && publicPlaylist != null) - { - await Task.Delay(1000); - await PlayNextSong(); - } - await Task.Delay(1000); } diff --git a/superstar.csproj b/superstar.csproj index 56e49f6..f97cac6 100644 --- a/superstar.csproj +++ b/superstar.csproj @@ -4,7 +4,7 @@ Exe net8.0-windows Images/superstar.ico - superstar + superstar_1.2 superstar enable enable