0

我正在尝试创建一个同时播放多种声音的 Windows Phone 应用程序。为此,我正在使用 SharpDX 库。

        private XAudio2 xAudio;
        private MasteringVoice masteringVoice;
        private AudioBuffer buffer;
        private SourceVoice PlaySound(string strPath)
        {
            try
            {

                if (masteringVoice == null)
                {
                    xAudio = new XAudio2();
                    masteringVoice = new MasteringVoice(xAudio);
                }
                using (var nativeFileStream = new NativeFileStream(strPath, NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read))
                {
                    using (SoundStream stream = new SoundStream(nativeFileStream))
                    {
                        var waveFormat = stream.Format;

                        buffer = new AudioBuffer
                        {
                            Stream = stream.ToDataStream(),
                            AudioBytes = (int)stream.Length,
                            Flags = BufferFlags.EndOfStream
                        };

                        var sourceVoice = new SourceVoice(xAudio, waveFormat, true);

                        sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);

                        if (buffer != null)
                        {
                            GC.SuppressFinalize(buffer);
                        }

                        return sourceVoice;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

在播放一些声音后,它会在 line buffer = new AudioBuffer 处引发 System.OutOfMemoryException 异常。如何解决此问题并释放内存?

4

0 回答 0