その1

Framework 2.0になったらサウンド再生も簡単らしいけど、今はCLRにないのでめんどくさ〜´〜`
winmm.dll呼ぶ方法が普通みたいじゃが、ストリームでやるときめんどくさそうなのでDirectXでやるのじゃ!まず、オーソドックスな奴から。


using Microsoft.DirectX.AudioVideoPlayback;

string soundFileName = "SUCCESS.WAV";
Audio audio = new Audio(soundFileName);
audio.Play();

ファイル名で与えられる時はらくちんまん。でも、ストリーム渡す時はこれだとダメなのら´〜`
まずはファイル名指定でのDirectSoundバージョン。

using Microsoft.DirectX.DirectSound;

private Device device = null;
private SecondaryBuffer sound = null;
string soundFileName = "SUCCESS.WAV";

BufferDescription desc = new BufferDescription();
desc.ControlPan = true;
desc.GlobalFocus = true;

device = new Device();
device.SetCooperativeLevel(this, CooperativeLevel.Normal);
sound = new SecondaryBuffer(soundFileName, desc, device);
sound.Play(0, BufferPlayFlags.Default);

さて、本題。アセンブリに埋め込んだファイルを再生するのだ。

using System.Reflection;
using Microsoft.DirectX.DirectSound;

// 現在実行中のアセンブリを取得
Assembly thisExe = Assembly.GetExecutingAssembly();
string assemblyName = thisExe.GetName().Name;
string soundFileName = "SUCCESS.WAV";
//埋め込みファイルのストリームを取得
Stream stream = thisExe.GetManifestResourceStream(assemblyName + "." + soundFileName);

BufferDescription desc = new BufferDescription();
desc.ControlPan = true;
desc.GlobalFocus = true;

device = new Device();
device.SetCooperativeLevel(this, CooperativeLevel.Normal);
sound = new SecondaryBuffer(stream, desc, device);
//ストリームを閉じる!
stream.Close();
sound.Play(0, BufferPlayFlags.Default);

半日がかりでなんとかいけた!