OKIEWARDOYO

C#.NET No.25-2

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