简单的 NAudio 播放

发布于 2024-11-23 18:04:36 字数 83 浏览 2 评论 0原文

如何让 NAudio 播放嵌入资源“WaterforMGC.delfino.wav”?不过,我不需要整个 .cs 文件,只想要一些我可以学习的简单内容。

How do I make NAudio play the embedded resource 'WaterforMGC.delfino.wav?' I don't want a whole .cs file though, just something simple that I can learn from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白日梦 2024-11-30 18:04:36

到目前为止,答案过于复杂,问题只是如何播放 .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();
    }

The answer sofar is overly complicated, question is only how to play a .wav file.

Easiest NAudio way I know to play a .wav file is this,

 public void PlaySound(string fileName)
    {
        var input = new NAudio.Wave.AudioFileReader(fileName);
        var w = new NAudio.Wave.WaveOut();
        w.Init(input);
        w.Play();
    }
错々过的事 2024-11-30 18:04:36

这边走:


    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();
                }
            }
        }
    }

this way:


    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();
                }
            }
        }
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文