使用C#播放音效(提示聲音)的4種方式

使用C#播放音效(提示聲音)的4種方式

使用C#播放音效(提示聲音)的4種方式


資料來源: https://blog.kkbruce.net/2019/03/csharpformusicplay.html#.YNUtk-gzaUk

https://dotblogs.com.tw/hung-chin/2012/03/31/71186


01.Win32 API Beep

[DllImport("kernel32.dll")]
private static extern int Beep(int dwFreq, int dwDuration);

public enum Music
{
 Do = 523,
 Re = 587,
 Mi = 659,
 Fa = 698,
 So = 784,
 La = 880,
 Ti = 988,
 Do2 = 1046
}
// 小蜜峰
Beep((int)Music.Do, 300);
Beep((int)Music.Do, 300);
Beep((int)Music.So, 300);
Beep((int)Music.So, 300);
Beep((int)Music.La, 300);
Beep((int)Music.La, 300);
Beep((int)Music.So, 600);

Beep((int)Music.Fa, 300);
Beep((int)Music.Fa, 300);
Beep((int)Music.Mi, 300);
Beep((int)Music.Mi, 300);
Beep((int)Music.Re, 300);
Beep((int)Music.Re, 300);
Beep((int)Music.Do, 600);

Beep((int)Music.So, 300);
Beep((int)Music.So, 300);
Beep((int)Music.Fa, 300);
Beep((int)Music.Fa, 300);
Beep((int)Music.Mi, 300);
Beep((int)Music.Mi, 300);
Beep((int)Music.Re, 600);

Beep((int)Music.So, 300);
Beep((int)Music.So, 300);
Beep((int)Music.Fa, 300);
Beep((int)Music.Fa, 300);
Beep((int)Music.Mi, 300);
Beep((int)Music.Mi, 300);
Beep((int)Music.Re, 600);

Beep((int)Music.Do, 300);
Beep((int)Music.Do, 300);
Beep((int)Music.So, 300);
Beep((int)Music.So, 300);
Beep((int)Music.La, 300);
Beep((int)Music.La, 300);
Beep((int)Music.So, 600);

Beep((int)Music.Fa, 300);
Beep((int)Music.Fa, 300);
Beep((int)Music.Mi, 300);
Beep((int)Music.Mi, 300);
Beep((int)Music.Re, 300);
Beep((int)Music.Re, 300);
Beep((int)Music.Do, 600);


02.SystemSounds 類別

//系統音效
SystemSounds.Asterisk.Play();
SystemSounds.Beep.Play();
SystemSounds.Exclamation.Play();
SystemSounds.Hand.Play();
SystemSounds.Question.Play();


03.SoundPlayer 類別 [WAV檔案]

using System.Media;

SoundPlayer player = new SoundPlayer();

player.SoundLocation = "媒體檔案路徑01";
player.LoadAsync();
player.PlaySync();

player.SoundLocation = "媒體檔案路徑02";
player.Load();
player.PlaySync();


04.WMPLib.dll [mp3檔案] 

//01.先加入參考 Windows Media Player的COM元件
//02.程式開頭 加上
	using WMPLib;
//03.實際完整程式碼
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Text;
	using System.Windows.Forms;
	using System.IO;
	using System.Threading;
	using System.Reflection;
	using WMPLib;
	namespace ResourceMP3
	{
		public partial class Form1 : Form
		{
			public Form1()
			{
				InitializeComponent();
				WindowsMediaPlayer wmp = new WindowsMediaPlayer();
				Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ResourceMP3.tada.mp3"); //命名空間.mp3檔名
				using (Stream output = new FileStream(Path.Combine(Application.StartupPath, "temp.mp3"), FileMode.Create))
				{
					byte[] buffer = new byte[32 * 1024];
					int read;
					while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
					{
						output.Write(buffer, 0, read);
					}
				}
				wmp.PlayStateChange += new _WMPOCXEvents_PlayStateChangeEventHandler(wmp_PlayStateChange);
				wmp.URL = Path.Combine(Application.StartupPath, "temp.mp3");
				wmp.settings.volume = 100;
				wmp.controls.play();
			}
			void wmp_PlayStateChange(int NewState)
			{
				if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
				{
					this.Close();
				}
			}
			private void Form1_Shown(object sender, EventArgs e)
			{
				if (File.Exists(Path.Combine(Application.StartupPath, "temp.mp3"))==true)
				{
				File.Delete(Path.Combine(Application.StartupPath, "temp.mp3"));
				}
			}
		}
	}

心得: 我會選擇02和03方案

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *