document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace NAudio_Project
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private NAudio.Wave.BlockAlignReductionStream stream = null; //
  15.         private NAudio.Wave.DirectSoundOut output = null;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void buttonOpen_Click(object sender, EventArgs e)
  23.         {
  24.             OpenFileDialog windowDialog = new OpenFileDialog();
  25.             windowDialog.Filter = "Audio files (*.mp3;*.wav)|*.mp3;*.wav;";
  26.             windowDialog.Title = "Open audio file";
  27.             if (windowDialog.ShowDialog() == DialogResult.OK)
  28.             {
  29.                 DisposeWave();//kita stop sound sebelumnya
  30.                 if (windowDialog.FileName.EndsWith(".mp3"))
  31.                 {
  32.                     NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(windowDialog.FileName));
  33.                     stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
  34.                 }
  35.                 else if (windowDialog.FileName.EndsWith(".wav"))
  36.                 {
  37.                     NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(windowDialog.FileName));
  38.                     stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
  39.                 }
  40.                 else throw new InvalidOperationException("Not supported file");
  41.  
  42.                 output = new NAudio.Wave.DirectSoundOut();
  43.                 output.Init(stream);
  44.                 output.Play();
  45.  
  46.                 buttonPlayPause.Enabled = true;
  47.                 textBoxFilename.Text = windowDialog.FileName;
  48.             }
  49.         }
  50.  
  51.         private void buttonPlayPause_Click(object sender, EventArgs e)
  52.         {
  53.             if (output != null)
  54.             {
  55.                 if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
  56.                 else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();
  57.  
  58.             }
  59.            
  60.         }
  61.  
  62.         //kita buat method untuk men-stop sound sebelumnya
  63.         private void DisposeWave()
  64.         {
  65.             if (output != null)
  66.             {
  67.                 if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
  68.                 output.Dispose();
  69.                 output = null;
  70.             }
  71.  
  72.             if (stream != null)
  73.             {
  74.                 stream.Dispose();
  75.                 stream = null;
  76.             }
  77.         }
  78.     }
  79. }
');