This project has moved. For the latest updates, please go here.

AcmStream Dispose was not called

Sep 13, 2011 at 4:28 AM

when the mp3 file is played, it does so smoothly initially(for about 4-5secs), but then a error message appears, saying : "AcmStream Dispose was not called."

The code is using the codes from the samples of NAudio available on the documentation page. Here's the complete .cs file, to make the situation clearer.

 

//*************************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NAudio;
using NAudio.Wave;
using NAudio.CoreAudioApi;
using System.Windows.Forms;

namespace PlayAudio
{
    public partial class Form1 : Form
    {

        //Declarations required for audio out and the MP3 stream
        IWavePlayer waveOutDevice;
        WaveStream mainOutputStream;
        WaveChannel32 volumeStream;
        string selectedFile;

        public Form1()
        {
            InitializeComponent();
            //waveOutDevice = new WasapiOut(AudioClientShareMode.Shared, 100);
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty( openFileDialog1.FileName ))
            {
                mainOutputStream = CreateInputStream(openFileDialog1.FileName);
                waveOutDevice = new WasapiOut(AudioClientShareMode.Shared, 100);
                waveOutDevice.Init(mainOutputStream);
                waveOutDevice.Play();
            }
        }

        private WaveStream CreateInputStream(string fileName)
        {
            WaveChannel32 inputStream;
            if (fileName.EndsWith(".mp3"))
            {
                WaveStream mp3Reader = new Mp3FileReader(fileName);
                inputStream = new WaveChannel32(mp3Reader);
            }
            else
            {
                throw new InvalidOperationException("Unsupported extension");
            }
            volumeStream = inputStream;
            return volumeStream;
        }

        private void CloseWaveOut()
        {
            if (waveOutDevice != null)
            {
                waveOutDevice.Stop();
            }
            if (mainOutputStream != null)
            {
                // this one really closes the file and ACM conversion
                volumeStream.Close();
                volumeStream = null;
                // this one does the metering stream
                mainOutputStream.Close();
                mainOutputStream = null;
            }
            if (waveOutDevice != null)
            {
                waveOutDevice.Dispose();
                waveOutDevice = null;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseWaveOut();
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                selectedFile = openFileDialog1.FileName;
        }

        private void bStop_Click(object sender, EventArgs e)
        {
            waveOutDevice.Stop();
            waveOutDevice = null;
            volumeStream = null;
            //CloseWaveOut();
        }
    }
}

 

 

 

//*************************************************************************************

// The winform design contains 3 buttons(btnPlay, btnStop and btnFileSelect) and nothing else (try to create a new winforms project to regenerate the same error in exactly the same context).

 

So, what might be wrong/missing in the code above? Thanks.

Sep 13, 2011 at 4:29 AM
Edited Sep 13, 2011 at 4:31 AM

Try also pressing Stop and then Play again, after playback has begun. It generates the same message.

Dec 24, 2011 at 2:11 AM

I seem to have exactly the same problem.

Looking at the code, it could happen if MP3FileReader is created but not disposed.

This doesn't seem to be the case I have. 

I am simply going to disable that assert for now.

Dec 26, 2011 at 8:10 PM

seems like the mp3Reader is not disposed.

Create another class fiel containing the mp3Reader and dispose it.