接上一篇,這次加了一個瀏覽本地wav檔案的功能 介面稍稍做了點小改動
程式碼
View Code
using System;
using System.Windows.Forms;
using System.Media;
namespace alarmClock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SoundPlayer player= new SoundPlayer();
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
//綁定到combobox
for (int i = 0; i <= 23; i++)
{
cmbHour.Items.Add(i);
}
for (int j = 0; j < 60; j++)
{
cmbMinute.Items.Add(j);
}
//綁定鈴聲
cmbRing.Items.Add("步步高音樂.wav");
cmbRing.Items.Add("背景音樂.wav");
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Interval = 1000;
lblNow.Text = DateTime.Now.ToString();
}
private void btnPreview_Click(object sender, EventArgs e)
{
playJudge();
}
/// <summary>
/// 判斷播放本地還是系統內建的
/// </summary>
private void playJudge()
{
if (cmbRing.Text==""&&txtUpload.Text=="")
{
MessageBox.Show("請選擇系統鈴聲或本地鈴聲");
return;
}
if (cmbRing.Text == "" || txtUpload.Text != "")
{
playSound(txtUpload.Text);
}
if (cmbRing.Text != "" || txtUpload.Text == "")
{
playSound(cmbRing.Text);
}
}
/// <summary>
/// 播放wav音效檔
/// </summary>
private void playSound(string path)
{
//用new出來的執行個體點SoundLocation指定想要播放的音樂名稱
player.SoundLocation = path;//(將播放音樂放在應用程式Debug目錄下)
player.Load();
//音樂播放
player.Play();
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (cmbHour.Text==""&&cmbMinute.Text=="")
{
MessageBox.Show("沒有設定鬧鈴的時刻");
return;
}
timer2.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Interval = 1000;
string h = cmbHour.Text;
string m = cmbMinute.Text;
string nowH =DateTime.Now.Hour.ToString();
string nowM = DateTime.Now.Minute.ToString();
if (h == nowH && m == nowM)
{
playJudge();
//開啟後停止線程
timer2.Stop();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
timer2.Stop();
player.Stop();
}
/// <summary>
/// 瀏覽本地鈴聲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//判斷是否開啟了"開啟檔案對話方塊"
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
string file=openFileDialog1.FileName;
if (file!=null)
{
//判斷上傳檔案是否為wav鈴聲
//D:\winformTest\alarmClcok\myClock\bin\Debug\步步高音樂.wav
string fileExtend = file.Substring(file.LastIndexOf(".") + 1).ToString().ToLower();
if (fileExtend!="wav")
{
MessageBox.Show("請選擇wav類型的檔案");
return;
}
txtUpload.Text = file;
}
}
}
}
}