using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NAudio_Project
{
public partial class Form1 : Form
{
private NAudio.Wave.BlockAlignReductionStream stream = null; //
private NAudio.Wave.DirectSoundOut output = null;
public Form1()
{
InitializeComponent();
}
private void buttonOpen_Click(object sender, EventArgs e)
{
OpenFileDialog windowDialog = new OpenFileDialog();
windowDialog.Filter = "Audio files (*.mp3;*.wav)|*.mp3;*.wav;";
windowDialog.Title = "Open audio file";
if (windowDialog.ShowDialog() == DialogResult.OK)
{
DisposeWave();//kita stop sound sebelumnya
if (windowDialog.FileName.EndsWith(".mp3"))
{
NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(windowDialog.FileName));
stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else if (windowDialog.FileName.EndsWith(".wav"))
{
NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(windowDialog.FileName));
stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else throw new InvalidOperationException("Not supported file");
output = new NAudio.Wave.DirectSoundOut();
output.Init(stream);
output.Play();
buttonPlayPause.Enabled = true;
textBoxFilename.Text = windowDialog.FileName;
}
}
private void buttonPlayPause_Click(object sender, EventArgs e)
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();
}
}
//kita buat method untuk men-stop sound sebelumnya
private void DisposeWave()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
output.Dispose();
output = null;
}
if (stream != null)
{
stream.Dispose();
stream = null;
}
}
}
}