如何使用NAudio以实时大小值同时播放两个音频文件

时间:2016-08-26 22:53:43

标签: c# audio real-time naudio

我第一次与NAudio合作,所以需要你的帮助

与我从默认音频源实时获取的方式相同,我怎么能得到两个音频wav或mp3文件,在这种情况下必须是直接输入我想,同时播放它与实时值。与下图所示的方式相同,但同时获取每个峰值大小。我在C#Windows窗体应用程序中用于此目标的内容和方式。我在源头寻找,但我一无所获,有些例子对我不起作用,似乎需要一些设置,所以检查一切,更好地获得实用和直接的例子或建议

using NAudio.CoreAudioApi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XX_9_REAL_TIME_AUDIO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
            var devices = enumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
            comboBox1.Items.AddRange(devices.ToArray());
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                var device = (MMDevice)comboBox1.SelectedItem;
                progressBar1.Value = (int) (Math.Round(device.AudioMeterInformation.MasterPeakValue * 100 + 0.5));


            }
        }
    }
}

这里我有wav文件,但不知道如何获得它的实时值以及如何同时播放另一个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;

namespace NAudioTest
{
    class Program
    {
        static IWavePlayer waveout;
        static WaveStream outputStream;
        static string filename = null;

        static void Main(string[] args)
        {
            waveout = new WaveOutEvent();

            filename ="C:\\folder\\track.wav";

            outputStream = CreateInputStream(filename);

            try
            {
                waveout.Init(outputStream);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while loading output");
                Console.WriteLine("Details: " + ex.Message);
                Console.ReadLine();
                return;
            }

            Console.WriteLine("Press [Enter] to start playback");
            Console.ReadLine();

            waveout.Play(); //this stops after 1 sec. should it play until i hit enter cause of the next line?

            Console.WriteLine("Press [Enter] to abort");
            Console.ReadLine();
            waveout.Dispose();
            Console.ReadLine();
        }


        static WaveStream CreateInputStream(string name)
        {
            WaveChannel32 inputStream;
            if (name.EndsWith(".wav"))
            {
                WaveStream readerStream = new WaveFileReader(name);
                if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
                {
                    readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream);
                    readerStream = new BlockAlignReductionStream(readerStream);
                }

                if (readerStream.WaveFormat.BitsPerSample != 16)
                {
                    var format = new WaveFormat(readerStream.WaveFormat.SampleRate, 16, readerStream.WaveFormat.Channels);
                    readerStream = new WaveFormatConversionStream(format, readerStream);
                }
                inputStream = new WaveChannel32(readerStream);
            }
            else
            {
                throw new InvalidOperationException("Invalid extension");
            }
            return inputStream;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

要同时播放多个文件(我认为你要求这样做),你应该为每个输入文件创建一个AudioFileReader。然后将每个作为输入连接到MixingSampleProvider。然后使用MixingSampleProvider

初始化输出音频设备