playing byte[] using naudio

时光怂恿深爱的人放手 提交于 2019-12-14 03:13:56

问题


  1. How can I convert an audio file to byte[] and play it using Naudio? (code will be appreciated)
  2. Also related to question above, how to play audio file in Resource using Naudio? For the second question I have this code:

    IWavePlayer waveOutDevice;
    IWaveProvider provider;
    
    public void PlaySound(byte[] sound)
    {
      waveoutDevice = new WaveOutEvent();
      provider = new RawSourceWaveStrem(new MemoryStream(sound), new WaveFormat();
      if (waveOutDevice != null)
      waveOutDevice.Stop();
      waveOutDevice.Init(provider);
      waveOutDevice.Play();
    
    }
    

    In my form constructor I then do something like -

    PlaySound(Properties.Resources.beepsound)
    

    beepsound being the sound file....but I just hear a noise when this method is called. What could be wrong?


回答1:


The WaveFileReader class can accept a Stream as a parameter, so you can use a MemoryStream to encapsulate a byte[] buffer whose contents you have loaded from a file.

Something like this:

byte[] fileContent = File.ReadAllBytes(@"C:\Some\File.wav");
var waveFileReader = new WaveFileReader(new MemoryStream(fileContent), true);

You can use GetManifestResourceStream or similar to get a stream for a resource and use that. If you want to reuse the streams, pass false as the second parameter which will stop them being disposed along with the WaveFileReader instance.



来源:https://stackoverflow.com/questions/33954614/playing-byte-using-naudio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!