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.BlockAlignReductionStream stream = null;//cause mp3 file
- 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 (.mp3)
- OpenFileDialog open = new OpenFileDialog();
- open.Filter = "MP3 file (*.mp3)|*.mp3";
- if (open.ShowDialog() != DialogResult.OK)
- {
- return;
- }
- DisposeWave();
- // use pcm data to restore mp3 file (because NAudio has no mp3 format code)
- NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(open.FileName));
- stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
- output = new NAudio.Wave.DirectSoundOut();
- // because the stream file is not a IWave file , so need to make a covertion
- output.Init(stream);
- 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 (stream != null)
- {
- stream.Dispose();
- stream = null;
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- DisposeWave();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement