NAudio AccessViolationException при воспроизведении звука - PullRequest
1 голос
/ 06 марта 2012

Я использую NAudio для воспроизведения нескольких звуков в моей программе. Проблема в том, что через некоторое время программа аварийно завершает работу, и кажется, что это вызвано звуком.

Я получаю System.AccessViolationException со следующей Stacktrace:

bei NAudio.Wave.DirectSoundOut.IDirectSoundBuffer.Stop()

bei NAudio.Wave.DirectSoundOut.StopPlayback()

bei NAudio.Wave.DirectSoundOut.PlaybackThreadFunc()

bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)

bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

bei System.Threading.ThreadHelper.ThreadStart()


Void Stop()

bei NAudio.Wave.DirectSoundOut.IDirectSoundBuffer.Stop()

bei NAudio.Wave.DirectSoundOut.StopPlayback()

bei NAudio.Wave.DirectSoundOut.PlaybackThreadFunc()

bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)

bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

bei System.Threading.ThreadHelper.ThreadStart()

Я использую следующий код:

class SLMBSoundOut : DirectSoundOut {
    public WaveStream stream;
    public SLMBSoundOut(String file, float vol = 1.0f)
        : base(200) {

        stream = CreateInputStream(file, vol);
        this.Init(stream);
    }

    private WaveStream CreateInputStream(string fileName, float vol) {
        WaveChannel32 inputStream;
        if (fileName.EndsWith(".wav")) {
            WaveStream readerStream = new WaveFileReader(fileName);
            if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm) {
                readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
                readerStream = new BlockAlignReductionStream(readerStream);
            }
            if (readerStream.WaveFormat.BitsPerSample != 16) {
                var format = new WaveFormat(readerStream.WaveFormat.SampleRate,
                   16, readerStream.WaveFormat.Channels);
                readerStream = new WaveFormatConversionStream(format, readerStream);
            }
            inputStream = new WaveChannel32(readerStream);
            inputStream.Volume = vol;

        } else {
            throw new InvalidOperationException("Unsupported extension");
        }
        inputStream.PadWithZeroes = false;
        return inputStream;
    }

}

Чтобы воспроизвести звук, я использую этот метод

private static void playResource(string res) {
        if (SLMB.Data.Constants.SOUND_ENABLED) {
            SLMBSoundOut player;
            player = new SLMBSoundOut(Directory.GetCurrentDirectory() + res);
            player.Play();
        }
    }

Кроме того, есть фоновая музыка, повторяющаяся с помощью PlaybackStopped-Event:

ambience.PlaybackStopped += new EventHandler(ambienceStopped);


private static void ambienceStopped(object sender, EventArgs args) {
        if (SLMB.Data.Constants.SOUND_ENABLED) {
            ambience.stream.Position = 0;
            ambience.Play();
        }
    }

Кто-нибудь имеет представление о том, что происходит не так?

...