Exemplo n.º 1
0
        /// <summary>
        /// The BackgroundWorker_DoWork.
        /// </summary>
        /// <param name="sender">The sender<see cref="object"/>.</param>
        /// <param name="e">The e<see cref="DoWorkEventArgs"/>.</param>
        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            progress = 0.0;

            // loop through selected ACM items
            foreach (int index in chkListBox.CheckedIndices)
            {
                if (this.IsDisposed)
                {
                    break;
                }

                ACM acm = acms[index];
                acm.WaveStream.Position = 0;                 // important! - always read stream from position 0.

                WaveFormat outWaveFormat = new WaveFormat(); // output format: 16bit, 44.1 kHz
                // Input is 16bit, 22050 Hz
                WaveFormatConversionStream waveFormatConversionStream = new WaveFormatConversionStream(outWaveFormat, acm.WaveStream);

                int    li      = acm.Tag.LastIndexOf('.'); // last index of dot; point is to remove extension to add the new one
                string outFile = outDir + Path.DirectorySeparatorChar + acm.Tag.Substring(0, li + 1) + audioFormat.ToString().ToLower();
                switch (audioFormat)
                {
                case AudioFormat.AAC:
                    MediaFoundationApi.Startup();
                    MediaFoundationEncoder.EncodeToAac(waveFormatConversionStream, outFile);
                    MediaFoundationApi.Shutdown();
                    break;

                case AudioFormat.MP3:
                    MediaFoundationApi.Startup();
                    MediaFoundationEncoder.EncodeToMp3(waveFormatConversionStream, outFile);
                    MediaFoundationApi.Shutdown();
                    break;

                case AudioFormat.WAV:
                    using (waveFormatConversionStream)
                    {
                        WaveFileWriter.CreateWaveFile(outFile, waveFormatConversionStream);
                    }
                    break;
                }
                progress += 100.0 / (double)chkListBox.CheckedItems.Count;
                backgroundWorker.ReportProgress((int)progress);
            }
            progress = 100.0;
        }
Exemplo n.º 2
0
        /// <summary>
        /// The ListBox_SelectedIndexChanged.
        /// </summary>
        /// <param name="sender">The sender<see cref="object"/>.</param>
        /// <param name="e">The e<see cref="EventArgs"/>.</param>
        private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = listBox.SelectedIndex;

            if (index != -1)
            {
                if (wo.PlaybackState != PlaybackState.Stopped)
                {
                    wo.Stop();
                }

                ACM acm = acms[index];
                if (acm.WaveStream != null && wo.PlaybackState == PlaybackState.Stopped)
                {
                    acm.WaveStream.Position = 0;
                    wo.Init(acm.WaveStream);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The BtnPlay_Click.
        /// </summary>
        /// <param name="sender">The sender<see cref="object"/>.</param>
        /// <param name="e">The e<see cref="EventArgs"/>.</param>
        private void BtnPlay_Click(object sender, EventArgs e)
        {
            if (wo.PlaybackState != PlaybackState.Paused)
            {
                wo.Stop();
            }

            int index = listBox.SelectedIndex;

            if (index != -1)
            {
                ACM acm = acms[index];
                if (acm.WaveStream != null &&
                    acm.WaveStream.Position == acm.WaveStream.Length)
                {
                    acm.WaveStream.Position = 0;
                }

                if (wo.PlaybackState != PlaybackState.Playing)
                {
                    wo.Play();
                }
            }
        }