【问题标题】:NAudio Play part of audio from recording fileNAudio 从录音文件中播放部分音频
【发布时间】:2018-11-17 05:13:50
【问题描述】:

使用 NAudio,在将音频录制到文件时,我需要使用 offsetSampleProvider.SkipOver / offsetSampleProvider.Take 播放该文件的一部分

问题是我无法使用 AudioFileReader 打开文件,因为它已被 WaveFileWriter 使用。

【问题讨论】:

    标签: c# audio-recording naudio


    【解决方案1】:

    我找到了解决办法。

    这里是源代码。

    using System;
    using System.Windows.Forms;
    using NAudio.Wave;
    using NAudio.Wave.SampleProviders;
    using static System.Environment;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            WaveFileWriter fileWriter;
            WaveOut outputSound;
            WaveIn waveSource;
            RawSourceWaveStream RSS;
            OffsetSampleProvider offsetSampleProvider;
            Stream sourceStream;
    
            string fileName = GetFolderPath(SpecialFolder.CommonApplicationData) + "\\temp.wav";
    
            public Form1()
            {
                InitializeComponent();
    
                outputSound = new WaveOut();
                waveSource = new WaveIn();
                waveSource.WaveFormat = new WaveFormat(8000, 16, 1);
                waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
                fileWriter = new WaveFileWriter(fileName, waveSource.WaveFormat);
                sourceStream = new MemoryStream();
    
                waveSource.StartRecording();
            }
    
            private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
            {
                fileWriter.Write(e.Buffer, 0, e.BytesRecorded);
                sourceStream.Write(e.Buffer, 0, e.BytesRecorded);
            }
    
            private void btnPlay_Click(object sender, EventArgs e)
            {
                RSS = new RawSourceWaveStream(sourceStream, waveSource.WaveFormat);
                RSS.Position = 0;
                offsetSampleProvider = new OffsetSampleProvider(RSS.ToSampleProvider());
                offsetSampleProvider.SkipOver = TimeSpan.FromMilliseconds(0);
                offsetSampleProvider.Take = TimeSpan.FromMilliseconds(3000);
                outputSound.Init(offsetSampleProvider);
                outputSound.Play();
            }
        }
    }
    

    【讨论】:

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