我试图创建一个同时播放多个声音的Windows手机应用。为此,我使用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;
}
}
在一些播放声音计数后,它会在行缓冲区=新的AudioBuffer处抛出System.OutOfMemoryException异常。我该如何解决这个问题,并释放内存?