Advertisement
Guest User

Untitled

a guest
Jul 10th, 2013
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace WindowsFormsApplication2
  5. {
  6. public partial class Form1 : Form
  7. {
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. }
  12. //
  13. // declar NAudio variavle
  14. //
  15. private NAudio.Wave.WaveFileReader wave = null;
  16. private NAudio.Wave.DirectSoundOut output = null;
  17.  
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20.  
  21. }
  22. private void read_button_Click(object sender, EventArgs e)
  23. {
  24. // read audio file (.wav)
  25. OpenFileDialog open = new OpenFileDialog();
  26. open.Filter = "Wave file (*.wav)|*.wav";
  27. if (open.ShowDialog() != DialogResult.OK)
  28. {
  29. return;
  30. }
  31. DisposeWave();
  32. // open file into wave variable
  33. wave = new NAudio.Wave.WaveFileReader(open.FileName);
  34. output = new NAudio.Wave.DirectSoundOut();
  35. // because the wave file is not a IWave file , so need to make a covertion
  36. output.Init(new NAudio.Wave.WaveChannel32(wave));
  37. output.Play();
  38. // when music is playing ,the button can be clicked .
  39. pause_button.Enabled = true;
  40. }
  41. //
  42. // pause button
  43. //
  44. private void pause_button_Click(object sender, EventArgs e)
  45. {
  46. if(output != null)
  47. {
  48. //when music is plauing , then pause it.
  49. if(output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
  50. {
  51. output.Pause();
  52. }
  53. // when music is paused , then play it .
  54. else if(output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
  55. {
  56. output.Play();
  57. }
  58. }
  59. }
  60. private void DisposeWave()
  61. {
  62. if (output != null)
  63. {
  64. if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
  65. {
  66. output.Stop();
  67. }
  68. output.Dispose();
  69. output = null;
  70. }
  71. if(wave != null)
  72. {
  73. wave.Dispose();
  74. wave = null;
  75. }
  76. }
  77. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  78. {
  79. DisposeWave();
  80. }
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement