【问题标题】:How to play .mp3 file from online resources in C#?如何在 C# 中播放在线资源中的 .mp3 文件?
【发布时间】:2012-07-27 03:08:56
【问题描述】:

我的问题与question非常相似

我有音乐网址。像 http://site.com/audio.mp3 这样的网址。我要玩 此文件在线,如 youtube。你知道一个类或代码可以做什么 这个?

如何在线播放 mp3 而无需全部下载才能播放?

会创建播放文件缓存,但至少会立即播放文件

【问题讨论】:

    标签: c# winforms mp3


    【解决方案1】:

    我通过库 NAudio 找到了解决方案 http://naudio.codeplex.com/

    解决方案

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

    我在这里找到了答案 Play audio from a stream using C#

    【讨论】:

      【解决方案2】:

      IrKlang 等许多库都提供流式音频支持,如果您正在寻找最少的开发工作,我建议您看看它!

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

      【讨论】:

        猜你喜欢
        • 2011-03-21
        • 1970-01-01
        • 2013-08-20
        • 2010-09-30
        • 1970-01-01
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多