superstar_v2/PrimaryFormParts/PrimaryForm.SoundEffects.cs

179 lines
7.5 KiB
C#
Raw Normal View History

2025-04-07 16:54:10 +08:00
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()
{
2025-08-13 09:48:25 +08:00
var data = LoadBtnConfigData();
2025-04-07 16:54:10 +08:00
constructionButton = new Button
{
Name = "constructionButton",
};
ConfigureButton(constructionButton, 18, 68, 148, 64,
2025-08-13 09:48:25 +08:00
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["ConstruSite"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["ConstruSite"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["ConstruSite"])),
2025-06-24 10:35:01 +08:00
ConstructionButton_Click);
pictureBoxSceneSoundEffects.Controls.Add(constructionButton);
2025-04-07 16:54:10 +08:00
marketButton = new Button
{
Name = "marketButton",
};
ConfigureButton(marketButton, 179, 68, 148, 63,
2025-08-13 09:48:25 +08:00
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Market"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Market"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Market"])),
2025-06-24 10:35:01 +08:00
MarketButton_Click);
pictureBoxSceneSoundEffects.Controls.Add(marketButton);
2025-04-07 16:54:10 +08:00
drivingButton = new Button
{
Name = "drivingButton",
};
ConfigureButton(drivingButton, 18, 144, 148, 63,
2025-08-13 09:48:25 +08:00
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Drive"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Drive"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Drive"])),
2025-06-24 10:35:01 +08:00
DrivingButton_Click);
pictureBoxSceneSoundEffects.Controls.Add(drivingButton);
2025-04-07 16:54:10 +08:00
airportButton = new Button
{
Name = "airportButton",
};
ConfigureButton(airportButton, 179, 144, 148, 63,
2025-08-13 09:48:25 +08:00
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Airport"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Airport"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Airport"])),
2025-06-24 10:35:01 +08:00
AirportButton_Click);
pictureBoxSceneSoundEffects.Controls.Add(airportButton);
2025-04-07 16:54:10 +08:00
officeButton = new Button
{
Name = "officeButton",
};
ConfigureButton(officeButton, 18, 220, 148, 64,
2025-08-13 09:48:25 +08:00
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Office"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Office"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Office"])),
2025-06-24 10:35:01 +08:00
OfficeButton_Click);
pictureBoxSceneSoundEffects.Controls.Add(officeButton);
2025-04-07 16:54:10 +08:00
closeButton = new Button
{
Name = "closeButton",
};
2025-06-24 10:35:01 +08:00
ConfigureButton(closeButton, 179, 220, 150, 63,
2025-08-13 09:48:25 +08:00
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Close"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Close"])),
new Bitmap(Path.Combine(serverPath, data["SoundEffect"]["Close"])),
2025-06-24 10:35:01 +08:00
CloseButton_Click);
pictureBoxSceneSoundEffects.Controls.Add(closeButton);
2025-04-07 16:54:10 +08:00
}
private void SoundEffectButton_Click(object sender, EventArgs e)
{
2025-08-13 09:48:25 +08:00
var data = LoadBtnConfigData();
SetPictureBoxToggleLightAndButtonsVisibility(false);
CloseUI(pictureBoxQRCode);
2025-04-07 16:54:10 +08:00
if (!pictureBoxSceneSoundEffects.Visible)
{
2025-08-13 09:48:25 +08:00
ShowImageOnPictureBoxSceneSoundEffects(Path.Combine(serverPath, data["SoundEffect"]["SoundEffectBaseUI"]));
2025-04-07 16:54:10 +08:00
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()
{
2025-06-24 10:35:01 +08:00
mediaPlayer.URL = Path.Combine(Application.StartupPath, "sounds", "zs.m4a");
2025-04-07 16:54:10 +08:00
mediaPlayer.controls.play();
}
2025-06-24 10:35:01 +08:00
2025-04-07 16:54:10 +08:00
private void ShowImageOnPictureBoxSceneSoundEffects(string imagePath)
{
2025-06-24 10:35:01 +08:00
Bitmap originalImage = new Bitmap(imagePath);
Rectangle cropArea = new Rectangle(856, 422, 342, 295);
pictureBoxSceneSoundEffects.Image = originalImage;
2025-06-24 10:35:01 +08:00
ResizeAndPositionPictureBox(pictureBoxSceneSoundEffects, cropArea.X, cropArea.Y, cropArea.Width, cropArea.Height);
pictureBoxSceneSoundEffects.Visible = true;
2025-04-07 16:54:10 +08:00
}
private void TogglePictureBoxSceneSoundEffectsButtonsVisibility()
{
2025-04-07 16:54:10 +08:00
bool areButtonsVisible = pictureBoxSceneSoundEffects.Visible;
SetPictureBoxSceneSoundEffectsAndButtonsVisibility(!areButtonsVisible);
}
private void SetPictureBoxSceneSoundEffectsAndButtonsVisibility(bool isVisible)
{
2025-04-07 16:54:10 +08:00
pictureBoxSceneSoundEffects.Visible = isVisible;
if (isVisible)
{
SetUIVisible(pictureBoxSceneSoundEffects);
2025-04-07 16:54:10 +08:00
}
}
}
}