如何使用 NAudio 同时播放两个具有实时大小值的音频文件
How to play two audio files simultaneously with real time size value with 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;
}
}
}
要同时播放多个文件(这就是我认为您要执行的操作),您应该为每个输入文件创建一个 AudioFileReader
。然后将每个作为输入连接到 MixingSampleProvider
。然后用 MixingSampleProvider
初始化你的输出音频设备
第一次使用 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;
}
}
}
要同时播放多个文件(这就是我认为您要执行的操作),您应该为每个输入文件创建一个 AudioFileReader
。然后将每个作为输入连接到 MixingSampleProvider
。然后用 MixingSampleProvider