private void buttonCompress_Click(object sender, System.EventArgs e) { if (File.Exists(textBoxOutFile.Text) && (MessageBox.Show(this, "Override the existing file?", "File exists", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)) { return; } try { progressBar.Value = 0; toolTip1.SetToolTip(progressBar, ""); this.Text = "Audio Compress"; Compressing = true; try { RefreshControls(); WaveStream InStr = new WaveStream(textBoxInFile.Text); try { Mp3Writer writer = new Mp3Writer(new FileStream(textBoxOutFile.Text, FileMode.Create), m_Config); try { byte[] buff = new byte[writer.OptimalBufferSize]; int read = 0; int actual = 0; long total = InStr.Length; Cursor.Current = Cursors.WaitCursor; try { while ((read = InStr.Read(buff, 0, buff.Length)) > 0) { Application.DoEvents(); writer.Write(buff, 0, read); actual += read; progressBar.Value = (int)(((long)actual * 100) / total); toolTip1.SetToolTip(progressBar, string.Format("{0}% compresssed", progressBar.Value)); this.Text = string.Format("Audio Compress - {0}% compresssed", progressBar.Value); Application.DoEvents(); } toolTip1.SetToolTip(progressBar, "Done"); this.Text = "Audio Compress - Done"; } finally { Cursor.Current = Cursors.Default; } } finally { writer.Close(); } } finally { InStr.Close(); } } finally { Compressing = false; RefreshControls(); } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "An exception has ocurred with the following message", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static void WmaToMp3(Stream wmaInputStream, Stream outputStream, Mp3WriterConfig mp3Format, int bufferMultiplier) { WmaToMp3Delegate convert = wmaStream => { var writer = new Mp3Writer(outputStream, mp3Format ?? new Mp3WriterConfig(wmaStream.Format, new BE_CONFIG(wmaStream.Format))); var buffer = new byte[writer.OptimalBufferSize*bufferMultiplier]; WriteToStream(writer, wmaStream, buffer); }; var tempStream = wmaInputStream as WmaStreamReader; if (tempStream != null) { convert(tempStream); } else { using (var wmaStream = new WmaStreamReader(wmaInputStream)) { convert(wmaStream); } } }