Advertisement
Guest User

Untitled

a guest
Jul 10th, 2013
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 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.BlockAlignReductionStream stream = null;//cause mp3 file
  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 (.mp3)
  25. OpenFileDialog open = new OpenFileDialog();
  26. open.Filter = "MP3 file (*.mp3)|*.mp3";
  27. if (open.ShowDialog() != DialogResult.OK)
  28. {
  29. return;
  30. }
  31. DisposeWave();
  32. // use pcm data to restore mp3 file (because NAudio has no mp3 format code)
  33. NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(open.FileName));
  34. stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
  35. output = new NAudio.Wave.DirectSoundOut();
  36. // because the stream file is not a IWave file , so need to make a covertion
  37. output.Init(stream);
  38. output.Play();
  39. // when music is playing ,the button can be clicked .
  40. pause_button.Enabled = true;
  41. }
  42. //
  43. // pause button
  44. //
  45. private void pause_button_Click(object sender, EventArgs e)
  46. {
  47. if(output != null)
  48. {
  49. //when music is plauing , then pause it.
  50. if(output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
  51. {
  52. output.Pause();
  53. }
  54. // when music is paused , then play it .
  55. else if(output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
  56. {
  57. output.Play();
  58. }
  59. }
  60. }
  61. private void DisposeWave()
  62. {
  63. if (output != null)
  64. {
  65. if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
  66. {
  67. output.Stop();
  68. }
  69. output.Dispose();
  70. output = null;
  71. }
  72. if (stream != null)
  73. {
  74. stream.Dispose();
  75. stream = null;
  76. }
  77. }
  78. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  79. {
  80. DisposeWave();
  81. }
  82.  
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement