using System.IO; using NAudio.Wave; using WMPLib; namespace DualScreenDemo { public partial class PrimaryForm : Form { private WindowsMediaPlayer mediaPlayer; private IWavePlayer waveOut; private AudioFileReader audioFileReader; private PictureBox pictureBoxSceneSoundEffects; private Button constructionButton; private Button marketButton; private Button drivingButton; private Button airportButton; private Button officeButton; private Button closeButton; private void InitializeMediaPlayer() { mediaPlayer = new WindowsMediaPlayer(); } private void InitializeSoundEffectButtons() { var data = LoadBtnConfigData(); constructionButton = new Button { Name = "constructionButton", }; ConfigureButton(constructionButton, 18, 68, 148, 64, new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["ConstruSite"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["ConstruSite"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["ConstruSite"])), ConstructionButton_Click); pictureBoxSceneSoundEffects.Controls.Add(constructionButton); marketButton = new Button { Name = "marketButton", }; ConfigureButton(marketButton, 179, 68, 148, 63, new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Market"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Market"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Market"])), MarketButton_Click); pictureBoxSceneSoundEffects.Controls.Add(marketButton); drivingButton = new Button { Name = "drivingButton", }; ConfigureButton(drivingButton, 18, 144, 148, 63, new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Drive"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Drive"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Drive"])), DrivingButton_Click); pictureBoxSceneSoundEffects.Controls.Add(drivingButton); airportButton = new Button { Name = "airportButton", }; ConfigureButton(airportButton, 179, 144, 148, 63, new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Airport"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Airport"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Airport"])), AirportButton_Click); pictureBoxSceneSoundEffects.Controls.Add(airportButton); officeButton = new Button { Name = "officeButton", }; ConfigureButton(officeButton, 18, 220, 148, 64, new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Office"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Office"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Office"])), OfficeButton_Click); pictureBoxSceneSoundEffects.Controls.Add(officeButton); closeButton = new Button { Name = "closeButton", }; ConfigureButton(closeButton, 179, 220, 150, 63, new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Close"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Close"])), new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Close"])), CloseButton_Click); pictureBoxSceneSoundEffects.Controls.Add(closeButton); } private void SoundEffectButton_Click(object sender, EventArgs e) { var data = LoadBtnConfigData(); SetPictureBoxToggleLightAndButtonsVisibility(false); CloseUI(pictureBoxQRCode); if (!pictureBoxSceneSoundEffects.Visible) { ShowImageOnPictureBoxSceneSoundEffects(Path.Combine(serverPath, data["SoundEffect"]["SoundEffectBaseUI"])); SetPictureBoxSceneSoundEffectsAndButtonsVisibility(true); } else { TogglePictureBoxSceneSoundEffectsButtonsVisibility(); } } private void ConstructionButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\1857.mp3"); private void MarketButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\13472_Audio Trimmer.mp3"); private void DrivingButton_Click(object sender, EventArgs e) => PlaySound(@"sounds\kc1.mp3"); private void AirportButton_Click(object sender, EventArgs e) => PlayMediaSound(@"sounds\xm2401.m4a"); private void OfficeButton_Click(object sender, EventArgs e) => PlayMediaSound(@"sounds\y1640.m4a"); private void CloseButton_Click(object sender, EventArgs e) => TogglePictureBoxSceneSoundEffectsButtonsVisibility(); private void PlaySound(string filePath) { waveOut?.Dispose(); audioFileReader?.Dispose(); waveOut = new WaveOutEvent(); audioFileReader = new AudioFileReader(Path.Combine(Application.StartupPath, filePath)); waveOut.Init(audioFileReader); waveOut.Play(); } private void PlayMediaSound(string filePath) { mediaPlayer.URL = Path.Combine(Application.StartupPath, filePath); mediaPlayer.controls.play(); } public void PlayApplauseSound() { mediaPlayer.URL = Path.Combine(Application.StartupPath, "sounds", "zs.m4a"); mediaPlayer.controls.play(); } private void ShowImageOnPictureBoxSceneSoundEffects(string imagePath) { Bitmap originalImage = new Bitmap(imagePath); Rectangle cropArea = new Rectangle(856, 422, 342, 295); pictureBoxSceneSoundEffects.Image = originalImage; ResizeAndPositionPictureBox(pictureBoxSceneSoundEffects, cropArea.X, cropArea.Y, cropArea.Width, cropArea.Height); pictureBoxSceneSoundEffects.Visible = true; } private void TogglePictureBoxSceneSoundEffectsButtonsVisibility() { bool areButtonsVisible = pictureBoxSceneSoundEffects.Visible; SetPictureBoxSceneSoundEffectsAndButtonsVisibility(!areButtonsVisible); } private void SetPictureBoxSceneSoundEffectsAndButtonsVisibility(bool isVisible) { pictureBoxSceneSoundEffects.Visible = isVisible; if (isVisible) { SetUIVisible(pictureBoxSceneSoundEffects); } } } }