【问题标题】:How can I save a music network stream to a mp3 file?如何将音乐网络流保存为 mp3 文件?
【发布时间】:2014-05-03 06:58:21
【问题描述】:

如何使用 NAudio 将流(以 byte[] 采样并编码为 PCM)保存到 mp3 文件?我已经听说过新的 Nadio Lame 编码器,但并不真正了解它。

我已经在网上搜索过,但并没有真正找到有用的东西。

我现在可以播放的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NAudio;

namespace PLAYER
{
    class NAudioPlayer : Player
    {
        NAudio.Wave.BufferedWaveProvider buf;
        NAudio.Wave.IWavePlayer player;
        int times = 0;

        public int EnqueueSamples(int channels, int rate, byte[] samples, int frames)
        {
            if (buf == null)
            {
                buf = new NAudio.Wave.BufferedWaveProvider(new   NAudio.Wave.WaveFormat(rate, channels));
                NAudio.Wave.WaveOut dso = new NAudio.Wave.WaveOut();
                dso.Init(buffer);
                dso.Play();
                player = dso;
            }
            int space = buf.BufferLength - buf.BufferedBytes;
            if (space > samples.Length)
            {
                if (times == 0)
                times = (times + 1) % 100;
                buf.AddSamples(samples, 0, samples.Length);
                return frames;
            }
            return 0;
        }
        public void Reset()
        {
            if (buffer != null) buffer.ClearBuffer();
        }
}

你能帮我提供一个代码sn-p吗? 或者只是告诉我在哪里可以找到有用的 sn-p?

【问题讨论】:

    标签: c# mp3 bytearray naudio pcm


    【解决方案1】:

    您可以使用NAudio.Lame 中的LameMP3FileWriter 进行编码,方法是将其视为Stream 并向其写入样本。

    这是一个现成的例子:

        LameMP3FileWriter mp3writer = null;
        WaveFormat mp3format = null;
    
        public void StartMP3Encoding(string filename, WaveFormat format)
        {
            if (mp3writer != null)
                StopMP3Encoding();
    
            mp3format = format;
            mp3writer = new LameMP3FileWriter(filename, format, 128);
        }
    
        public void StopMP3Encoding()
        {
            if (mp3writer != null)
            {
                mp3writer.Dispose();
                mp3writer = null;
            }
        }
    
        public int EnqueueSamples(int channels, int rate, byte[] samples, int frames)
        {
            if (mp3writer != null && mp3format.Channels == channels)
                mp3writer.Write(samples, 0, samples.Length);
    
            return frames;
        }
    

    当您想开始录制 MP3 文件时,请调用 StartMP3Encoding 并使用要录制到的文件名和数据格式。完成后,调用StopMP3Encoding 关闭编码器。在这两者之间,写下您收到的样本以及何时到达。

    LAME 编码器的时间并不宝贵,因此您可以随心所欲地向其传输数据,或者尽可能快地向其传输数据。

    LAME 编码器对输入采样率和输出比特率的组合有一些限制,但它可以处理标准值的公平变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多