2014-03-24 131 views
1

我的音頻應用程序出現問題。我正在使用nAudio庫。 因此,當我在waveViewer控件上不使用「SamplesPerPixel」方法播放聲音時,一切正常,但是當我想爲此方法分配一個值時。聲音,當我演奏時,從意想不到的時間開始,約4秒完成。後來。當我調整WaveForm的大小以調整大小時音頻播放停止

這裏是我的代碼:

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

回答

0

我認爲這可以幫助你

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

+3

對於未來的讀者:將代碼格式化爲代碼(由4個空格標識)並解釋爲什麼這有助於解決問題。謝謝 –

相關問題