今回は再生と停止です。すぐ出来ます。
using System; using System.Windows.Forms; using System.IO; using System.Drawing; using Microsoft.DirectX.DirectSound; using DS = Microsoft.DirectX.DirectSound; using System.Runtime.InteropServices; using System.Threading; namespace Sound2 { /// <summary> /// Class1 の概要の説明です。 /// </summary> public class SoundForm:Form { private Device device_; private DS.SecondaryBuffer secondaryBufferA_; //開くファイル private string fileName =@"i:\temp\test2.wav"; private Button playButton; private Button stopButton; public SoundForm() { this.Text="DirectSound"; DSInitialize(); playButton =new Button(); playButton.Location = new Point(10,10); playButton.Size =new Size(80,30); playButton.Click+=new EventHandler(playButton_Click); playButton.Name = "playButton"; playButton.Text = "play"; this.Controls.Add(playButton); stopButton =new Button(); stopButton.Location = new Point(110,10); stopButton.Size =new Size(80,30); stopButton.Click+=new EventHandler(stopButton_Click); stopButton.Name = "stopButton"; stopButton.Text = "stop"; this.Controls.Add(stopButton); } protected override void Dispose( bool disposing ) { if( disposing ) { if(secondaryBufferA_ != null) { secondaryBufferA_.Stop(); secondaryBufferA_.Dispose(); } if(device_ != null) { device_.Dispose(); } } base.Dispose( disposing ); } private void DSInitialize() { device_ =new Device(); //協調レベルの設定 device_.SetCooperativeLevel(this,CooperativeLevel.Normal); //バッファについての情報設定クラス BufferDescription desc=new BufferDescription(); //セカンダリバッファを作成 secondaryBufferA_ = new SecondaryBuffer(fileName,desc,device_); } private void playButton_Click(object sender, EventArgs e) { secondaryBufferA_.Play(0,0); } private void stopButton_Click(object sender, EventArgs e) { secondaryBufferA_.Stop(); } } public class Program { public static void Main() { Application.Run(new SoundForm()); } } }
停止は「stop()」で行うことが出来ます。停止は一時停止になります。また、再生が終わると自動で巻き戻しされます。