Download WavePlay.zip version 1.0.0.0, last updated 23/09/2012 (17.31 KB)

Download
  • md5: c843de73a133efff768069ba12dd1057
using System;
using System.IO;
using System.Media;
using System.Windows.Forms;

namespace WavePlay
{
  public partial class MainForm
    : Form
  {
    private SoundPlayer _currentSound;

    public MainForm()
    {
      this.InitializeComponent();
    }

    private void exitButton_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void filesListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (autoPlayCheckBox.Checked)
        this.PlayFile();
    }

    private void ListFiles()
    {
      string path;

      filesListBox.BeginUpdate();
      filesListBox.Items.Clear();
      this.StopSound();

      path = pathTextBox.Text;
      if (Directory.Exists(path))
      {
        foreach (string fileName in Directory.GetFiles(path, "*.wav"))
          filesListBox.Items.Add(Path.GetFileName(fileName));
      }

      filesListBox.EndUpdate();
    }

    private void loopCheckBox_CheckedChanged(object sender, EventArgs e)
    {
      this.PlayFile();
    }

    private void pathBrowseButton_Click(object sender, EventArgs e)
    {
      using (FolderBrowserDialog dialog = new FolderBrowserDialog())
      {
        dialog.SelectedPath = pathTextBox.Text;
        dialog.Description = "Select the folder containing audio files";

        if (dialog.ShowDialog(this) == DialogResult.OK)
          pathTextBox.Text = dialog.SelectedPath;
      }
    }

    private void pathTextBox_TextChanged(object sender, EventArgs e)
    {
      this.ListFiles();
    }

    private void playButton_Click(object sender, EventArgs e)
    {
      this.PlayFile();
    }

    private void PlayFile()
    {
      this.StopSound();

      if (filesListBox.SelectedIndex != -1)
      {
        string fileName;

        fileName = Path.Combine(pathTextBox.Text, (string)filesListBox.Items[filesListBox.SelectedIndex]);

        if (File.Exists(fileName))
        {
          try
          {
            _currentSound = new SoundPlayer(fileName);

            if (loopCheckBox.Checked)
              _currentSound.PlayLooping();
            else
              _currentSound.Play();
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
        }
      }
    }

    private void stopButton_Click(object sender, EventArgs e)
    {
      this.StopSound();
    }

    private void StopSound()
    {
      if (_currentSound != null)
      {
        _currentSound.Stop();
        _currentSound.Dispose();
        _currentSound = null;
      }
    }
  }
}

Donate

Donate