首先,我正在使用NAudio,所以需要您的帮助
与我从默认音频源实时到达此处的方式相同,我如何获得两个音频wav或mp3文件,在这种情况下,我想它必须是直接输入,才能与实时值同时播放。如下图所示,但同时要从每个图中获取电流峰的大小。我必须在C#Windows Form应用程序中为此目的使用什么以及如何使用它。我一直在寻找源代码,但没有发现任何结果,有些示例对我不起作用,似乎需要一些设置,因此请检查所有内容,更好地获取实际且直接的示例或建议
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;
}
}
}
最佳答案
要同时播放多个文件(我想您要这样做),您应该为每个输入文件创建一个AudioFileReader
。然后将每个作为输入连接到MixingSampleProvider
。然后使用MixingSampleProvider
初始化您的输出音频设备
关于c# - 如何使用NAudio同时播放具有实时大小值的两个音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39175660/