当我调整WaveForm的大小以形成大小时,音频播放停止

问题描述 投票:1回答:1

我的音频应用程序有问题。我正在使用nAudio库。因此,当我在waveViewer控件上不使用“SamplesPerPixel”方法播放声音时,一切都运行正常,但是当我想为此方法赋值时。声音,当我播放时从意外时间开始并完成约4秒。后来。

这是我的代码:

namespace AudioMixer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int bytesPerSample;
        int samples;
        int samplespixel;
        private NAudio.Wave.WaveStream pcm = null;

        private NAudio.Wave.BlockAlignReductionStream stream = null;
        private NAudio.Wave.DirectSoundOut output = null;

        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "Audio File (*.wav;*.mp3)|*.wav;*.mp3;";

            if (dialog.ShowDialog() != DialogResult.OK) return;

            DisposeWave();

            if (dialog.FileName.EndsWith(".mp3"))
            {
                pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(dialog.FileName));
                stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
            } 
            else if (dialog.FileName.EndsWith(".wav")) 
            {
                pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(dialog.FileName));
                stream = new NAudio.Wave.BlockAlignReductionStream(pcm);       
            }

            output = new NAudio.Wave.DirectSoundOut();
            output.Init(stream);
            output.Stop();

            waveViewer1.WaveStream = stream;
            bytesPerSample = (pcm.WaveFormat.BitsPerSample / 8) * pcm.WaveFormat.Channels;
            samples = (int)(pcm.Length / bytesPerSample);
            samplespixel = samples / this.Width;
            waveViewer1.SamplesPerPixel = samplespixel;

            opened_file_name.Text = dialog.FileName;
            play_button.Visible = true;
            play_button.Enabled = true;           
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            waveViewer1.SamplesPerPixel = samplespixel;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            opened_file_name.Text = "Audio file not opened, choose one from Your computer";
            play_button.Visible = false;
        }

        private void play_button_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();
                }
                else if (output.PlaybackState == NAudio.Wave.PlaybackState.Stopped)
                {
                    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();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
c# .net audio naudio
1个回答
0
投票

我想这可能会对你有所帮助

waveViewer1.SamplesPerPixel = 400; waveViewer1.StartPosition = 400;

© www.soinside.com 2019 - 2024. All rights reserved.