NAudio从流?播放音频 [英] NAudio from stream? play audio
问题描述
我正在尝试将我的.net项目转换为.net核心(IoT核心).除了我的"system.media"块外,其他所有东西都在工作,因为.net核心根本不是system.media附带的.我对此的选择显然是使用NAudio.但是,我不知道如何使NAudio从流中播放音频.
I am trying to convert my .net project to .net core (IoT core). Everything is working except my "system.media" block as .net core doesn't come with system.media at all. My options to this is to use NAudio apparently. However, I have no clue how to make NAudio play audio from a Stream.
这是我正在尝试使用NAudio转换的当前代码.有什么建议?
This is the current code I am trying to convert with NAudio. Any suggestions?
public void PlayAudio(object sender, GenericEventArgs<Stream> args)
{
SoundPlayer player = new SoundPlayer(args.EventData);
player.PlaySync();
args.EventData.Dispose();
}
我尝试使用此代码,但没有成功.
I tried with this code but got no success.
using (Stream ms = new MemoryStream())
{
using (Stream stream = WebRequest.Create(url)
.GetResponse().GetResponseStream())
{
byte[] buffer = new byte[32768];
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
ms.Position = 0;
using (WaveStream blockAlignedStream =
new BlockAlignReductionStream(
WaveFormatConversionStream.CreatePcmStream(
new Mp3FileReader(ms))))
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(blockAlignedStream);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing )
{
System.Threading.Thread.Sleep(100);
}
}
}
}
推荐答案
Have you tried using the AudioGraph APIs? These are a great way to play audio on IoT Core. I've used it with success on a Raspberry Pi.
这篇关于NAudio从流?播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!