【问题标题】:How to play two audio files simultaneously with real time size value with NAudio如何使用 NAudio 同时播放具有实时大小值的两个音频文件
【发布时间】:2016-08-26 22:53:42
【问题描述】:

我是第一次使用 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;
        }
    }
}

【问题讨论】:

    标签: c# audio real-time naudio


    【解决方案1】:

    要同时播放多个文件(我认为您要求这样做),您应该为每个输入文件创建一个AudioFileReader。然后将它们中的每一个作为输入连接到MixingSampleProvider。然后用MixingSampleProvider初始化你的输出音频设备

    【讨论】:

    • 首先要对您的出色工作表示感谢!希望现在我可以按照此说明解决我的问题。我看到的所有教程都没有显示我想要的东西,我在naudio.codeplex.com 上找不到与这个问题相对应的一些演示项目,因为这是我第一次处理 NAudio,只是不知道该使用什么。所以也许,如果你能给我一些有用的链接来了解它的工作原理,那就太好了。并再次感谢您的反馈
    猜你喜欢
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    相关资源
    最近更新 更多