使用NAudio,转换MP3以将其存储到Memorystream中,从内存流中读取并播放wave文件

时间:2011-03-28 11:42:42

标签: c# naudio

在NAudio WaveFileWriter类中,我实现了这个方法。

public static void CreateWaveFile(Stream waveMemoryStream, WaveStream stream)
{
    using (WaveFileWriter writer = 
        new WaveFileWriter(waveMemoryStream, stream.WaveFormat))
    {
        byte[] buffer = new byte[stream.WaveFormat.SampleRate * stream.WaveFormat.Channels * 16];
        while (true)
        {
            int bytesRead = stream.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0)
                break;
            writer.WriteData(buffer, 0, bytesRead);
        }
    }
}

// --------------------------------------------- -------------------------------------------------- ------------------------------

在我的appplication里面我有这个代码

public void TrimWavFile(string inPath, MemoryStream outPath, TimeSpan cutFromStart, TimeSpan cutFromEnd)
{
    using (Mp3FileReader mp3reader = new Mp3FileReader(inPath))
    {
        WaveStream stream = WaveFormatConversionStream.CreatePcmStream(mp3reader);
        WaveFormat format = stream.WaveFormat;
        MemoryStream waveStream = new MemoryStream();
        WaveFileWriter.CreateWaveFile(waveStream, stream);
        Stream rawStream = new MemoryStream(waveStream.ToArray());

        using (WaveFileReader reader = new WaveFileReader(rawStream))
        {
            using (WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat))
            {
                int bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000;
                int startPos = (int)cutFromStart.TotalMilliseconds * bytesPerMillisecond;
                startPos = startPos - startPos % reader.WaveFormat.BlockAlign;
                int endBytes = (int)cutFromEnd.TotalMilliseconds * bytesPerMillisecond;
                endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
                int endPos = (int)reader.Length - endBytes; TrimWavFile(reader, writer, startPos, endPos);
            }
        }
    }
}

private void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos)
{
    reader.Position = startPos;
    byte[] buffer = new byte[1024];

    while (reader.Position < endPos)
    {
        int bytesRequired = (int)(endPos - reader.Position);
        if (bytesRequired > 0)
        {
            int bytesToRead = Math.Min(bytesRequired, buffer.Length);
            int bytesRead = reader.Read(buffer, 0, bytesToRead);
            if (bytesRead > 0)
            {
                writer.WriteData(buffer, 0, bytesRead);
            }
        }
    }
}

private void button5_Click(object sender, EventArgs e)
{
    TimeSpan start = new TimeSpan(0, 3, 0);
    TimeSpan end = new TimeSpan(0, 2, 0);

    MemoryStream temp = new MemoryStream();

    TrimWavFile(filename, temp, start, end);

    Stream rawStream = new MemoryStream(temp.ToArray());
    RawSourceWaveStream rawSourceStream = new RawSourceWaveStream(rawStream,new WaveFormat());
    WaveStream wavestream = rawSourceStream;
    WaveStream stream = WaveFormatConversionStream.CreatePcmStream(new WaveFileReader(wavestream));
    waveOut.Init(stream);
    waveOut.Play();
}

=============================================== ======================================

当我点击按钮时,我想读取一个MP3文件,将其转换为波形文件,将转换后的波形文件存储到内存流中,

拆分已转换的波形文件,然后播放波形文件,并将其保存到我的Hardrive中。

0 个答案:

没有答案