How to play .mp3 file from online resources in C#?

前端 未结 2 923
遇见更好的自我
遇见更好的自我 2021-01-23 07:29

my question is very similar to this question

I have music urls. Urls like http://site.com/audio.mp3 . I want play this file online, like youtube. Do you

相关标签:
2条回答
  • 2021-01-23 07:53

    Many libraries like IrKlang provide streaming audio support, if you're looking for minimal development effort I'd suggest you take a look at it!

    http://www.ambiera.com/irrklang/

    0 讨论(0)
  • 2021-01-23 07:57

    I found the solution with the library NAudio http://naudio.codeplex.com/

    SOLUTION

    public static void PlayMp3FromUrl(string url)
    {
        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);
                    }
                }
            }
        }
    }`
    

    I FOUND THE ANSWER HERE Play audio from a stream using C#

    0 讨论(0)
提交回复
热议问题