我正在编写一个包含简单 MP3/WAV 媒体播放器区域的程序。
我遵循了 NAudio 示例程序中包含的代码,这是我创建的用于封装可播放音频文件的类:
public sealed class PlaybackContext : IDisposable {
private String _fileName;
private DirectSoundOut _dsoundOut;
private WaveStream _waveStream;
public PlaybackContext(String fileName) {
_fileName = fileName;
_dsoundOut = new DirectSoundOut( DirectSoundOut.DSDEVID_DefaultPlayback );
String ext = Path.GetExtension( fileName );
if( ext.Equals(".mp3", StringComparison.OrdinalIgnoreCase) ) {
_waveStream = new Mp3FileReader( fileName );
} else if( ext.Equals(".wav", StringComparison.OrdinalIgnoreCase) ) {
_waveStream = new WaveFileReader( fileName );
switch( _waveStream.WaveFormat.Encoding ) {
case WaveFormatEncoding.Pcm:
case WaveFormatEncoding.IeeeFloat:
break;
default:
_waveStream = new BlockAlignReductionStream( WaveFormatConversionStream.CreatePcmStream( _waveStream ) );
break;
}
}
_dsoundOut.Init( _waveStream );
}
public void Play() {
_dsoundOut.Play();
}
// hidden are the Pause/Stop/Dispose methods
它适用于 MP3 文件,但是当我尝试播放任何 Wave 文件时,我得到了这个异常:
Object contains non-primitive or non-blittable data.
System.ArgumentException
at System.Runtime.InteropServices.GCHandle.InternalAlloc(Object value, GCHandleType type)
at System.Runtime.InteropServices.GCHandle.Alloc(Object value, GCHandleType type)
at NAudio.Wave.DirectSoundOut.InitializeDirectSound()
at NAudio.Wave.DirectSoundOut.PlaybackThreadFunc()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
我在 Google 上找不到任何东西,而且我的代码是为 x86 编译的(所以这不是 64 位问题)。否则我被卡住了。
有任何想法吗?