使用naudio播放byte [] [英] playing byte[] using naudio
问题描述
- 如何将音频文件转换为byte []并使用Naudio播放? (代码将不胜感激)
-
也与上面的问题有关,如何使用Naudio在Resource中播放音频文件? 对于第二个问题,我有以下代码:
- How can I convert an audio file to byte[] and play it using Naudio? (code will be appreciated)
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?
推荐答案
WaveFileReader
类可以接受Stream
作为参数,因此您可以使用MemoryStream
封装byte[]
缓冲区,其内容如下已从文件加载.
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.
类似这样的东西:
byte[] fileContent = File.ReadAllBytes(@"C:\Some\File.wav");
var waveFileReader = new WaveFileReader(new MemoryStream(fileContent), true);
您可以使用GetManifestResourceStream
或类似方法来获取资源流并使用它.如果要重用这些流,请将false
作为第二个参数传递,这将阻止它们与WaveFileReader
实例一起处置.
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.
这篇关于使用naudio播放byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!