Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //
- // declar NAudio variavle
- //
- private NAudio.Wave.WaveFileReader wave = null;
- private NAudio.Wave.DirectSoundOut output = null;
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void read_button_Click(object sender, EventArgs e)
- {
- // read audio file (.wav)
- OpenFileDialog open = new OpenFileDialog();
- open.Filter = "Wave file (*.wav)|*.wav";
- if (open.ShowDialog() != DialogResult.OK)
- {
- return;
- }
- DisposeWave();
- // open file into wave variable
- wave = new NAudio.Wave.WaveFileReader(open.FileName);
- output = new NAudio.Wave.DirectSoundOut();
- // because the wave file is not a IWave file , so need to make a covertion
- output.Init(new NAudio.Wave.WaveChannel32(wave));
- output.Play();
- // when music is playing ,the button can be clicked .
- pause_button.Enabled = true;
- }
- //
- // pause button
- //
- private void pause_button_Click(object sender, EventArgs e)
- {
- if(output != null)
- {
- //when music is plauing , then pause it.
- if(output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
- {
- output.Pause();
- }
- // when music is paused , then play it .
- else if(output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
- {
- output.Play();
- }
- }
- }
- private void DisposeWave()
- {
- if (output != null)
- {
- if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
- {
- output.Stop();
- }
- output.Dispose();
- output = null;
- }
- if(wave != null)
- {
- wave.Dispose();
- wave = null;
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- DisposeWave();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement