OKIEWARDOYO

C#.NET No.25-1

Mar 8th, 2013
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  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.WaveFileReader wave = null;//nanti diganti
  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 = "Wave file (*.wav)|*.wav;";//nanti diganti
  26.             windowDialog.Title = "Open wav file";
  27.             if (windowDialog.ShowDialog() == DialogResult.OK)
  28.             {
  29.                 DisposeWave();//kita stop sound sebelumnya
  30.  
  31.                 wave = new NAudio.Wave.WaveFileReader(windowDialog.FileName);//nanti diganti
  32.  
  33.                 output = new NAudio.Wave.DirectSoundOut();
  34.                 output.Init(new NAudio.Wave.WaveChannel32(wave));
  35.                 output.Play();
  36.  
  37.                 buttonPlayPause.Enabled = true;
  38.                 textBoxFilename.Text = windowDialog.FileName;
  39.             }
  40.         }
  41.  
  42.         private void buttonPlayPause_Click(object sender, EventArgs e)
  43.         {
  44.             if (output != null)
  45.             {
  46.                 if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
  47.                 else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();
  48.  
  49.             }
  50.            
  51.         }
  52.  
  53.         //kita buat method untuk men-stop sound sebelumnya
  54.         private void DisposeWave()
  55.         {
  56.             if (output != null)
  57.             {
  58.                 if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
  59.                 output.Dispose();
  60.                 output = null;
  61.             }
  62.  
  63.             if (wave != null)
  64.             {
  65.                 wave.Dispose();
  66.                 wave = null;
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment