墨香年少 32 发布于 2023年2月20日 核心代码 using System; using System.Collections.Generic; using System.Threading; using System.Windows.Forms; namespace TimerPlay { public partial class Form1 : Form { List<string> mp3_list = new List<string>(); int max_loop_count = 0; int looped_count = -1; int play_i = 0; int start_ts = 0; int close_ts = 0; int used_ts = 0; bool started = false; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { started = false; comboBox_play_mod.SelectedIndex = 0; axWindowsMediaPlayer1.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(axMediaPlayer_PlayStateChange); } private void axMediaPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) { if (mp3_list.Count < 0) { return; } string txt = ""; switch (e.newState) { case 0: txt = "Undefined"; break; case 1: txt = "Stopped"; break; case 2: txt = "Paused"; break; case 3: txt = "Playing"; play_i++; if (play_i >= mp3_list.Count) { play_i = 0; } if (1 == play_i) { looped_count++; label_looped_count.Text = "已经循环: " + looped_count.ToString() + "次"; if (looped_count >= max_loop_count) { axWindowsMediaPlayer1.Ctlcontrols.stop(); MessageBox.Show("播放完毕,已经循环:"+ looped_count.ToString()+"次", "提示"); } } break; case 4: txt = "ScanForward"; break; case 5: txt = "ScanReverse"; break; case 6: txt = "Buffering"; break; case 7: txt = "Waiting"; break; case 8: txt = "MediaEnded"; break; case 9: txt = "Transitioning"; break; case 10: txt = "Ready"; break; case 11: txt = "Reconnecting"; break; case 12: txt = "Last"; break; default: txt = ("Unknown State: " + e.newState.ToString()); break; } if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded) { axWindowsMediaPlayer1.URL = mp3_list[play_i]; } if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsReady) { if (axWindowsMediaPlayer1.URL == mp3_list[play_i]) { try { label_active_name.Text = "["+ (play_i + 1).ToString() +"/"+ mp3_list.Count.ToString() + "]"+GetFileName(mp3_list[play_i]); axWindowsMediaPlayer1.Ctlcontrols.play(); } catch (Exception ex) { } } } } private string GetFileName(string path) { string[] ps = path.Split('\\'); return ps[ps.Length-1]; } //选择音乐 private void button_add_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Multiselect = true; ofd.Filter = "mp3音频文件|*.mp3"; DialogResult dr = ofd.ShowDialog(); if (dr == DialogResult.OK) { string[] arr = ofd.FileNames; foreach (string file in arr) { mp3_list.Add(file); } for (int m = 0; m < mp3_list.Count; m++) { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = m; this.dataGridView1.Rows[index].Cells[1].Value = mp3_list[m]; axWindowsMediaPlayer1.currentPlaylist.insertItem(m, axWindowsMediaPlayer1.newMedia(mp3_list[m])); } label_selected_tip.Text = "已选择 " + mp3_list.Count.ToString() + " 首音乐"; } } } private void button_start_Click(object sender, EventArgs e) { if (mp3_list.Count < 1) { MessageBox.Show("没有选择任何mp3,无法开始"); } else { button_add.Enabled = false; button_start.Enabled = false; tb_loop_count.Enabled = false; comboBox_play_mod.Enabled = false; dateTimePicker_start.Enabled = false; dateTimePicker_close.Enabled = false; this.dataGridView1.Enabled = false; max_loop_count = int.Parse(tb_loop_count.Text); string start = dateTimePicker_start.Text; start_ts = datetime_ts(start); string close = dateTimePicker_close.Text; close_ts = datetime_ts(close); timer1.Enabled = true; } } int datetime_ts(string dt) { DateTime time = Convert.ToDateTime(dt); System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 TimeSpan ts = time - startTime; int timestamp = (int)Convert.ToInt64(ts.TotalSeconds); return timestamp; } public int GetTimeStamp10() { return (int)new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); } public void play_mp3() { try { axWindowsMediaPlayer1.Ctlcontrols.play(); } catch (Exception e) { MessageBox.Show(e.Message.ToString(), "Exception catched!"); } } private void exit_app(object sender, FormClosingEventArgs e) { const string message = "确定关闭程序吗?"; const string caption = "请确认"; var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Dispose(true); Application.Exit(); } else { e.Cancel = true; } } private void sync_timer(object sender, EventArgs e) { label_curr_time.Text = "当前时间:"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); int now = GetTimeStamp10(); if (now >= start_ts) { if (!started) { started = true; label_left_time.Text = ""; used_ts = 0; label_active_name.Text = "[1/" + mp3_list.Count.ToString() + "]" + GetFileName(mp3_list[0]); play_mp3(); } } else { int time = start_ts - now; label_left_time.Text = "距离开始:" + SecondToHour(time); } if (started) { used_ts++; label_used_time.Text = "已用时:"+SecondToHour(used_ts); if (now >= close_ts) { timer1.Enabled = false; started = false; axWindowsMediaPlayer1.Ctlcontrols.stop(); MessageBox.Show("已经到设定的结束时间:" + dateTimePicker_close.Text, "结束提示"); } else { label_close_tip.Text = "距离结束:"+SecondToHour(close_ts - now); } } } public string SecondToHour(int t) { int second = 0; int minute = 0; int hour = 0; if (t > 0) { second = t % 60; } if (t > 60) { minute = t / 60; } if (minute > 60) { hour = minute / 60; } return (hour + "时" + minute + "分" + second + "秒"); } } } 目之所及,皆是回忆,心之所想,皆是过往 分享这篇帖子 链接帖子 分享到其他站点