2015-04-27 86 views
1
public partial class Form1 : Form 
{ 
    SecondarySoundBuffer m_DSoundBuffer; 
    DirectSound m_DirectSound; 
    string fileName = @"F:\\guitar-classical-E-octave0.wav"; 
    public void setting() 
    { 
     WaveStream waveFile = new WaveStream(fileName); 
     SoundBufferDescription desc = new SoundBufferDescription(); 
     desc.SizeInBytes = (int)waveFile.Length; 
     desc.Flags = BufferFlags.None; 
     desc.Format = waveFile.Format; 
     m_DirectSound = new DirectSound(); 
     m_DirectSound.SetCooperativeLevel(this.Handle, CooperativeLevel.Priority); 
     m_DSoundBuffer = new SecondarySoundBuffer(m_DirectSound, desc); 
    } 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     setting(); 
     m_DSoundBuffer.Play(0, 0); 
    } 


} 

我是第一個參加SlimDX的人。如何使用SlimDX.DirectSound播放波形文件?

我試圖播放一個wave文件。但它不起作用!

也許有什麼錯?

使用的WinForms,SlimDX.DirectSound,SlimDX.Mutimedia

+0

DirectSound是古老的。您應該使用可以與SlimDX一起使用的XAudio2。 –

+0

@ChuckWalbourn THX –

回答

0

我做到了。

緩衝區必須寫!

public void setting() 
    { 
     m_DirectSound = new DirectSound(); 
     m_DirectSound.SetCooperativeLevel(this.Handle, CooperativeLevel.Priority); 
     m_DirectSound.IsDefaultPool = false; 

     using (WaveStream waveFile = new WaveStream(fileName)) 
     { 

      SoundBufferDescription desc = new SoundBufferDescription(); 
      desc.SizeInBytes = (int)waveFile.Length; 
      desc.Flags = BufferFlags.None; 
      desc.Format = waveFile.Format; 

      m_DSoundBuffer = new SecondarySoundBuffer(m_DirectSound, desc); 
      byte[] data = new byte[desc.SizeInBytes]; 
      waveFile.Read(data, 0, (int)waveFile.Length); 
      m_DSoundBuffer.Write(data, 0, LockFlags.None); 
     }