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

时间:2014-03-24 10:45:47

标签: c# .net audio naudio

我的音频应用程序出了问题。我正在使用nAudio库。 因此,当我播放声音而不使用" SamplesPerPixel"我的waveViewer控件上的方法,一切都很完美,但是当我想为这个方法赋值时。声音,当我播放时从意外时间开始并完成约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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为这可能会对你有所帮助

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