简体   繁体   English

将WaveForm调整为表格大小时,音频播放停止

[英]Audio playing stopped when i resize a WaveForm to form size

I have a problem with my audio application. 我的音频应用程序有问题。 I'm using nAudio library. 我正在使用nAudio库。 So when I play a sound without using "SamplesPerPixel" method on my waveViewer control, everything works perfectly, but when I want assign a value to this method. 因此,当我在waveViewer控件上未使用“ SamplesPerPixel”方法播放声音时,一切工作正常,但是当我想为该方法分配值时。 The Sound, when I play it starts from unexpected time and finish about 4sec. 播放声音时,声音会从意外的时间开始,大约持续4秒。 later. 后来。

Here is my code: 这是我的代码:

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

i think this may help you 我认为这可能对您有帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM