private void Play() { try { TimeSpan now = new TimeSpan(DateTime.Now.Ticks); player.Play(now); while (player != null && !stopPlaying) { lock (playerLock) { if (player.Playing) { player.Update(now); int scrollVal = (int)player.Elapsed.TotalSeconds; if (scrollVal != scrSeek.Value) { SetScrollValue(scrollVal); } } else { if (scrSeek.Value != 0) { SetScrollValue(0); } } } Thread.Sleep(1); now = new TimeSpan(DateTime.Now.Ticks); } lock (playerLock) { if (player.Playing) { player.Stop(); //Console.WriteLine("Closed while playing, stopPlaying: " + stopPlaying + ", player was null: " + (player == null)); } else { //Console.WriteLine("Closed due to done playing"); } } } #if DEBUG catch (Exception e) { Console.WriteLine("Background thread terminated: " + e.ToString()); } #else catch { } #endif finally { player.CloseDevice(); player = null; } }
private void btnPlay_Click(object sender, EventArgs e) { if (!songtext1.IsValid()) { return; } if (bfilemodified) { bPlayNow = true; SaveFile(songtext1.FileName); return; } btnPlay.Image = Properties.Resources.btn_green_play; btnPause.Image = Properties.Resources.btn_black_pause; btnStop.Image = Properties.Resources.btn_black_stop; if (bneverplayed) { bneverplayed = false; LoadFileIntoPlayer(); } else { lock (playerLock) { if (player != null) { if (player.Playing && !player.Paused) { player.Stop(); } player.Play(new TimeSpan(MusicPlayer.Time.Ticks)); } } } }