如何让 NAudio 播放嵌入式资源“WaterforMGC.delfino.wav?” 不过,我不想要一个完整的 .cs 文件,只是我可以从中学习的一些简单的东西。
问问题
1032 次
2 回答
0
到目前为止的答案过于复杂,问题只是如何播放.wav文件。
我知道播放 .wav 文件的最简单的 NAudio 方式是这样的,
public void PlaySound(string fileName)
{
var input = new NAudio.Wave.AudioFileReader(fileName);
var w = new NAudio.Wave.WaveOut();
w.Init(input);
w.Play();
}
于 2020-12-28T17:14:09.417 回答
-2
这边走:
using System;
using System.Windows.Forms;
namespace playDataAudio {
public partial class Form1 : Form {
public Form1() { InitializeComponent();
}
using System;
using System.Windows.Forms;
namespace playDataAudio
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private NAudio.Wave.BlockAlignReductionStream stream = null;
private NAudio.Wave.DirectSoundOut output = null;
private void openFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Audio File (*.mp3;*.wav)|*.mp3;*.wav;";
if (open.ShowDialog() != DialogResult.OK) return;
DisposeWave();
if (open.FileName.EndsWith(".mp3"))
{
NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(open.FileName));
stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else if (open.FileName.EndsWith(".wav"))
{
NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else throw new InvalidOperationException("Not a correct audio file type.");
output = new NAudio.Wave.DirectSoundOut();
output.Init(stream);
output.Play();
pauseButton.Enabled = true;
}
private void pauseButton_Click(object sender, EventArgs e)
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();
}
}
private void DisposeWave()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
output.Dispose();
output = null;
}
if (stream != null)
{
stream.Dispose();
stream = null;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DisposeWave();
}
}
}
}
于 2015-04-23T20:18:39.070 回答