NAudio from stream? play audio

爷,独闯天下 提交于 2019-12-01 12:54:56

问题


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.

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);
                }
            }
        }
    }

回答1:


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.



来源:https://stackoverflow.com/questions/49701981/naudio-from-stream-play-audio

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