// 音楽再生〇〇〇 [System.Runtime.InteropServices.DllImport("winmm.dll")] private static extern int mciSendString(String command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback); private string aliasName = "BGM"; private void button1_Click(object sender, EventArgs e) { ongaku(); } private void ongaku() { string fileName = "C:\\Users\\音楽.mp3"; string cmd; cmd = "open \"" + fileName + "\" type mpegvideo alias " + aliasName; if (mciSendString(cmd, null, 0, IntPtr.Zero) != 0) return; cmd = "play " + aliasName + " repeat"; mciSendString(cmd, null, 0, IntPtr.Zero); cmd = "setaudio " + aliasName + " volume to 300"; mciSendString(cmd, null, 0, IntPtr.Zero); }
private void Play() { string cmd; //再生するファイル名 string fileName = Random.Shared.Next(1, 9) switch { 1 => "C2.wav", 2 => "F2.wav", 3 => "G2.wav", 4 => "C3.wav", 5 => "EF2.wav", 6 => "AF2.wav", 7 => "BF2.wav", _ => "EF3.wav" }; fileName = Application.StartupPath + @"Sound\" + fileName; string aliasName = $"Alias{DateTime.Now.Ticks}{Random.Shared.Next(1, 90000000)}"; mciSendString($"open \"{fileName}\" alias {aliasName}", null, 0, IntPtr.Zero); mciSendString($"play {aliasName}", null, 0, IntPtr.Zero); cmd = "setaudio " + aliasName + " volume to 200"; mciSendString(cmd, null, 0, IntPtr.Zero); label4.Text = mciSendString(cmd, null, 0, IntPtr.Zero).ToString(); }