title: C# Using SoundPlayer to Pause and Resume
body: <p>I am another person dabbling with C# and wanting to create a simple audio application that plays wav files loaded into the program via an upload program. My issue is that I need to get whatever audio file currently being played to pause with the track timer of the audio file being used when I start the audio file again via my Play button. I already have a global timer, 'baseTimer', that I think I can use to set the audio file, that was stopped, track duration point. However I do not know how to accomplish this nor do I really know how to use all of the mci commands yet.</p>
  
  <p>I have displayed all of my code for my main application... I also have read that I may need to utilize threading, but I've also read that it would be impossible to set an audio files track duration with a thread.</p>
  
  <pre><code>public partial class FrmMain : Form
  {
      public FrmMain()
      {
          InitializeComponent();
      }
  
      System.Timers.Timer baseTimer = new System.Timers.Timer();
  
      List&lt;string&gt; PlayList = new List&lt;string&gt;();
      List&lt;byte&gt; PlayList_byte;
      int soundNum = 0;
  
      private string music_PATH { get; set; }
      private string talk_PATH { get; set; }
      private byte Pause_TIME { get; set; }
      private string Pause_RADIO { get; set; }
  
      bool isStopped = new bool();
      bool isPaused = new bool();
  
      [DllImport("winmm.dll")]
      private static extern uint mciSendString(string command, StringBuilder returnValue, int returnLength, IntPtr winHandle);
  
      public static int GetSoundLength(string fileName)
      {
          StringBuilder lengthBuf = new StringBuilder(32);
  
          mciSendString(string.Format("open \"{0}\" type waveaudio alias wave", fileName), null, 0, IntPtr.Zero);
          mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, IntPtr.Zero);
          mciSendString("close wave", null, 0, IntPtr.Zero);
  
          int length = 0;
          int.TryParse(lengthBuf.ToString(), out length);
  
          return length;
      }
  
      private void SetPath()
      {
          music_PATH = @"..\\..\\commercial\list.txt";
          talk_PATH = @"..\\..\\main\list.txt";
          StreamReader myReader;
  
          using (myReader = new StreamReader(music_PATH))
          {
              while (myReader.Peek() != -1)
              {
                  string read = myReader.ReadLine();
                  PlayList.Add(read);
              }
          }
  
          using (myReader = new StreamReader(talk_PATH))
          {
              while (myReader.Peek() != -1)
              {
                  string read = myReader.ReadLine();
                  PlayList.Add(read);
              }
              myReader.Close();
          }
  
          foreach (string sound in PlayList)
          {
              soundNum++;
          }
      }
  
      private string CurrentSound()
      {
          try
          {
              Random _randx = new Random();
              int pick = _randx.Next(0, soundNum);
  
              string currentaudio = PlayList[pick];
              Pause_RADIO = currentaudio;
              return currentaudio;
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error: " + ex.Message);
          }
          return null;
      }
  
      string _SelectedSound = "";
  
      private string _Sound(string currentradio, string pattern)
      {
          foreach (Match entry in Regex.Matches(currentradio, pattern))
          {
              _SelectedSound = entry.Value.ToString();
          }
          if (_SelectedSound == "music")
          {
              return "commercial";
          }
          else if (_SelectedSound == "talk")
          {
              return "main";
          }
          return null;
      }
  
      private void _SetTimer(string currentradio, string pattern)
      {
          baseTimer.Interval = GetSoundLength(@"..\\..\\" + pattern + @"\" + currentradio);
      }
  
      private bool isRepeat(string lastradio, string currentradio)
      {
          if (lastradio == currentradio)
          {
              return true;
          }
          else
          {
              return false;
          }
      }
  
      private void baseTimerElasped(object sender, ElapsedEventArgs e)
      {
          Radio.FrmMain play = new Radio.FrmMain();
          play.PlayPlayer();
      }
  
      private void PlayPlayer()
      {
          MediaPlayer wavplayer;
          try
          {
              if (soundNum == 0)
              {
                  SetPath();
                  PlayPlayer();
              }
              else
              {
                  string currentradio = CurrentSound();
                  bool localcheck = isRepeat(_SelectedSound, currentradio);
                  if (localcheck == true)
                  {
                      PlayPlayer();
                  }
                  else
                  {
                      string Pattern = @"(music|talk)";
                      string selected = _Sound(currentradio, Pattern);
                      _SetTimer(currentradio, selected);
  
                      switch (selected)
                      {
                          case "commercial":
                              music_PATH = @"..\\..\\commercial\";
                              PlayList_byte = new List&lt;byte&gt;(File.ReadAllBytes(music_PATH + currentradio));
                              wavplayer = new MediaPlayer(PlayList_byte.GetRange(0, PlayList_byte.Count).ToArray());
                              wavplayer.Play();
                              baseTimer.Start();
                              break;
                          case "main":
                              talk_PATH = @"..\\..\\main\";
                              PlayList_byte = new List&lt;byte&gt;(File.ReadAllBytes(talk_PATH + currentradio));
                              wavplayer = new MediaPlayer(PlayList_byte.GetRange(0, PlayList_byte.Count).ToArray());
                              wavplayer.Play();
                              baseTimer.Start();
                              break;
                      }
                  }
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error: " + ex.Message + ex.StackTrace + ex.Source);
          }
      }
  
      private void PausePlayer()
      {
          MediaPlayer wavplayer = new MediaPlayer(PlayList_byte.GetRange(0, PlayList_byte.Count).ToArray());
          baseTimer.Stop();
  
          MessageBox.Show("Count: " + PlayList_byte.Count + "Pause_TIME: " + Pause_TIME + "\nPlaylist_byte" + PlayList_byte.ToString());
  
          try
          {
              switch (isPaused)
              {
                  case false:
                      isPaused = true;
                      wavplayer.Stop();
                      break;
                  case true:
                      isPaused = false;
                      string localcheck = _Sound(Pause_RADIO, @"(music|talk)");
                      switch (localcheck)
                      {
                          case "commercial":
                              music_PATH = @"..\\..\\commercial\";
                              wavplayer.Play(PlayList_byte.GetRange(PlayList_byte.Count - Pause_TIME, PlayList_byte.Count - Pause_TIME).ToArray());
                              break;
                          case "main":
                              talk_PATH = @"..\\..\\main\";
                              wavplayer.Play(PlayList_byte.GetRange(PlayList_byte.Count - Pause_TIME, PlayList_byte.Count - Pause_TIME).ToArray());
                              break;
                      }
                      break;
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error: " + ex.Message + ex.Data);
          }
      }
  
      private void btnPlay_Click(object sender, EventArgs e)
      {
          switch (isStopped)
          {
              case false:
                  isStopped = true;
                  btnPlay.Image = Image.FromFile(@"..\\..\\Pause.png");
                  lblPlay.Text = "Pause";
                  if (isPaused == false)
                  {
                      PlayPlayer();
                  }
                  else
                  {
                      PausePlayer();
                  }
                  break;
              case true:
                  isStopped = false;
                  btnPlay.Image = Image.FromFile(@"..\\..\\Play.png");
                  lblPlay.Text = "Play";
                  PausePlayer();
                  break;
          }
  
          baseTimer.Elapsed += new ElapsedEventHandler(baseTimerElasped);
      }
  
      private void btnNext_Click(object sender, EventArgs e)
      {
          PlayPlayer();
      }
  
      private void btnStop_Click(object sender, EventArgs e)
      {
          MediaPlayer wavplayer = new MediaPlayer();
          wavplayer.Stop();
          baseTimer.Stop();
      }
  
      private void btnQuit_Click(object sender, EventArgs e)
      {
          Application.Exit();
      }
  
      private void btnGetUpload_Click(object sender, EventArgs e)
      {
          Uploader FrmUpload = new Uploader();
          FrmUpload.Show();
          this.Hide();
      }
  }
  class MediaPlayer
  {
      SoundPlayer wavplayer;
  
      public MediaPlayer()
      {
          Stop();
      }
  
      public MediaPlayer(byte[] buffer)
      {
          MemoryStream memStream = new MemoryStream(buffer, true);
          wavplayer = new SoundPlayer(memStream);
      }
  
      public void Play()
      {
          wavplayer.Play();
      }
  
      public void Play(byte[] buffer)
      {
          wavplayer.Stream.Seek(0, SeekOrigin.Begin);
          wavplayer.Stream.Write(buffer, 0, buffer.Length);
          wavplayer.Play();
      }
  
      public void Stop()
      {
          wavplayer.Stop();
      }
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>For clarity currentaudio has a file as such "music3.wav" or "talk1.wav" in it.</p>