Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 57 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Media;
  7. using System.Windows.Forms;
  8.  
  9. namespace SoundApiExample
  10. {
  11.     public class SoundTestForm : System.Windows.Forms.Form
  12.     {
  13.         private System.Windows.Forms.Label label1;
  14.         private System.Windows.Forms.TextBox filepathTextbox;        
  15.         private System.Windows.Forms.Button playOnceSyncButton;
  16.         private System.Windows.Forms.Button playOnceAsyncButton;
  17.         private System.Windows.Forms.Button playLoopAsyncButton;
  18.         private System.Windows.Forms.Button selectFileButton;
  19.  
  20.         private System.Windows.Forms.Button stopButton;
  21.         private System.Windows.Forms.StatusBar statusBar;
  22.         private System.Windows.Forms.Button loadSyncButton;
  23.         private System.Windows.Forms.Button loadAsyncButton;        
  24.         private SoundPlayer player;
  25.  
  26.         public SoundTestForm()
  27.         {
  28.             // Initialize Forms Designer generated code.
  29.             InitializeComponent();
  30.  
  31.             // Disable playback controls until a valid .wav file
  32.             // is selected.
  33.             EnablePlaybackControls(false);
  34.  
  35.             // Set up the status bar and other controls.
  36.             InitializeControls();
  37.  
  38.             // Set up the SoundPlayer object.
  39.             InitializeSound();
  40.         }
  41.  
  42.         // Sets up the status bar and other controls.
  43.         private void InitializeControls()
  44.         {
  45.             // Set up the status bar.
  46.             StatusBarPanel panel = new StatusBarPanel();
  47.             panel.BorderStyle = StatusBarPanelBorderStyle.Sunken;
  48.             panel.Text = "Ready.";
  49.             panel.AutoSize = StatusBarPanelAutoSize.Spring;
  50.             this.statusBar.ShowPanels = true;
  51.             this.statusBar.Panels.Add(panel);
  52.         }
  53.  
  54.         // Sets up the SoundPlayer object.
  55.         private void InitializeSound()
  56.         {
  57.             // Create an instance of the SoundPlayer class.
  58.             player = new SoundPlayer();
  59.  
  60.             // Listen for the LoadCompleted event.
  61.             player.LoadCompleted += new AsyncCompletedEventHandler(player_LoadCompleted);
  62.  
  63.             // Listen for the SoundLocationChanged event.
  64.             player.SoundLocationChanged += new EventHandler(player_LocationChanged);
  65.         }
  66.  
  67.         private void selectFileButton_Click(object sender,
  68.             System.EventArgs e)
  69.         {
  70.             // Create a new OpenFileDialog.
  71.             OpenFileDialog dlg = new OpenFileDialog();
  72.  
  73.             // Make sure the dialog checks for existence of the
  74.             // selected file.
  75.             dlg.CheckFileExists = true;
  76.  
  77.             // Allow selection of .wav files only.
  78.             dlg.Filter = "WAV files (*.wav)|*.wav";
  79.             dlg.DefaultExt = ".wav";
  80.  
  81.             // Activate the file selection dialog.
  82.             if (dlg.ShowDialog() == DialogResult.OK)
  83.             {
  84.                 // Get the selected file's path from the dialog.
  85.                 this.filepathTextbox.Text = dlg.FileName;
  86.  
  87.                 // Assign the selected file's path to
  88.                 // the SoundPlayer object.  
  89.                 player.SoundLocation = filepathTextbox.Text;
  90.             }
  91.         }
  92.  
  93.         // Convenience method for setting message text in
  94.         // the status bar.
  95.         private void ReportStatus(string statusMessage)
  96.         {
  97.             // If the caller passed in a message...
  98.             if ((statusMessage != null) && (statusMessage != String.Empty))
  99.             {
  100.                 // ...post the caller's message to the status bar.
  101.                 this.statusBar.Panels[0].Text = statusMessage;
  102.             }
  103.         }
  104.  
  105.         // Enables and disables play controls.
  106.         private void EnablePlaybackControls(bool enabled)
  107.         {  
  108.             this.playOnceSyncButton.Enabled = enabled;
  109.             this.playOnceAsyncButton.Enabled = enabled;
  110.             this.playLoopAsyncButton.Enabled = enabled;
  111.             this.stopButton.Enabled = enabled;
  112.         }
  113.  
  114.         private void filepathTextbox_TextChanged(object sender,
  115.             EventArgs e)
  116.         {
  117.             // Disable playback controls until the new .wav is loaded.
  118.             EnablePlaybackControls(false);
  119.         }
  120.  
  121.         private void loadSyncButton_Click(object sender,
  122.             System.EventArgs e)
  123.         {  
  124.             // Disable playback controls until the .wav is
  125.             // successfully loaded. The LoadCompleted event
  126.             // handler will enable them.
  127.             EnablePlaybackControls(false);
  128.  
  129.             try
  130.             {
  131.                 // Assign the selected file's path to
  132.                 // the SoundPlayer object.  
  133.                 player.SoundLocation = filepathTextbox.Text;
  134.  
  135.                 // Load the .wav file.
  136.                 player.Load();
  137.             }
  138.             catch (Exception ex)
  139.             {
  140.                 ReportStatus(ex.Message);
  141.             }
  142.         }
  143.  
  144.         private void loadAsyncButton_Click(System.Object sender,
  145.             System.EventArgs e)
  146.         {
  147.             // Disable playback controls until the .wav is
  148.             // successfully loaded. The LoadCompleted event
  149.             // handler will enable them.
  150.             EnablePlaybackControls(false);
  151.  
  152.             try
  153.             {
  154.                 // Assign the selected file's path to
  155.                 // the SoundPlayer object.  
  156.                 player.SoundLocation = this.filepathTextbox.Text;
  157.  
  158.                 // Load the .wav file.
  159.                 player.LoadAsync();
  160.             }
  161.             catch (Exception ex)
  162.             {
  163.                 ReportStatus(ex.Message);
  164.             }
  165.         }
  166.  
  167.         // Synchronously plays the selected .wav file once.
  168.         // If the file is large, UI response will be visibly
  169.         // affected.
  170.         private void playOnceSyncButton_Click(object sender,
  171.             System.EventArgs e)
  172.         {  
  173.             ReportStatus("Playing .wav file synchronously.");
  174.             player.PlaySync();
  175.             ReportStatus("Finished playing .wav file synchronously.");
  176.         }
  177.  
  178.         // Asynchronously plays the selected .wav file once.
  179.         private void playOnceAsyncButton_Click(object sender,
  180.             System.EventArgs e)
  181.         {
  182.             ReportStatus("Playing .wav file asynchronously.");
  183.             player.Play();
  184.         }
  185.  
  186.         // Asynchronously plays the selected .wav file until the user
  187.         // clicks the Stop button.
  188.         private void playLoopAsyncButton_Click(object sender,
  189.             System.EventArgs e)
  190.         {
  191.             ReportStatus("Looping .wav file asynchronously.");
  192.             player.PlayLooping();
  193.         }
  194.  
  195.         // Stops the currently playing .wav file, if any.
  196.         private void stopButton_Click(System.Object sender,
  197.             System.EventArgs e)
  198.         {  
  199.             player.Stop();
  200.             ReportStatus("Stopped by user.");
  201.         }
  202.  
  203.         // Handler for the LoadCompleted event.
  204.         private void player_LoadCompleted(object sender,
  205.             AsyncCompletedEventArgs e)
  206.         {  
  207.             string message = String.Format("LoadCompleted: {0}",
  208.                 this.filepathTextbox.Text);
  209.             ReportStatus(message);
  210.             EnablePlaybackControls(true);
  211.         }
  212.  
  213.         // Handler for the SoundLocationChanged event.
  214.         private void player_LocationChanged(object sender, EventArgs e)
  215.         {  
  216.             string message = String.Format("SoundLocationChanged: {0}",
  217.                 player.SoundLocation);
  218.             ReportStatus(message);
  219.         }
  220.  
  221.         private void playSoundFromResource(object sender, EventArgs e)
  222.         {
  223.             System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
  224.             System.IO.Stream s = a.GetManifestResourceStream("<AssemblyName>.chimes.wav");
  225.             SoundPlayer player = new SoundPlayer(s);
  226.             player.Play();
  227.         }
  228.  
  229.         #region Windows Form Designer generated code
  230.         private void InitializeComponent()
  231.         {
  232.             this.filepathTextbox = new System.Windows.Forms.TextBox();
  233.             this.selectFileButton = new System.Windows.Forms.Button();
  234.             this.label1 = new System.Windows.Forms.Label();
  235.             this.loadSyncButton = new System.Windows.Forms.Button();
  236.             this.playOnceSyncButton = new System.Windows.Forms.Button();
  237.             this.playOnceAsyncButton = new System.Windows.Forms.Button();
  238.             this.stopButton = new System.Windows.Forms.Button();
  239.             this.playLoopAsyncButton = new System.Windows.Forms.Button();
  240.             this.statusBar = new System.Windows.Forms.StatusBar();
  241.             this.loadAsyncButton = new System.Windows.Forms.Button();
  242.             this.SuspendLayout();
  243.             //
  244.             // filepathTextbox
  245.             //
  246.             this.filepathTextbox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
  247.             this.filepathTextbox.Location = new System.Drawing.Point(7, 25);
  248.             this.filepathTextbox.Name = "filepathTextbox";
  249.             this.filepathTextbox.Size = new System.Drawing.Size(263, 20);
  250.             this.filepathTextbox.TabIndex = 1;
  251.             this.filepathTextbox.Text = "";
  252.             this.filepathTextbox.TextChanged += new System.EventHandler(this.filepathTextbox_TextChanged);
  253.             //
  254.             // selectFileButton
  255.             //
  256.             this.selectFileButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  257.             this.selectFileButton.Location = new System.Drawing.Point(276, 25);
  258.             this.selectFileButton.Name = "selectFileButton";
  259.             this.selectFileButton.Size = new System.Drawing.Size(23, 21);
  260.             this.selectFileButton.TabIndex = 2;
  261.             this.selectFileButton.Text = "...";
  262.             this.selectFileButton.Click += new System.EventHandler(this.selectFileButton_Click);
  263.             //
  264.             // label1
  265.             //
  266.             this.label1.Location = new System.Drawing.Point(7, 7);
  267.             this.label1.Name = "label1";
  268.             this.label1.Size = new System.Drawing.Size(145, 17);
  269.             this.label1.TabIndex = 3;
  270.             this.label1.Text = ".wav path or URL:";
  271.             //
  272.             // loadSyncButton
  273.             //
  274.             this.loadSyncButton.Location = new System.Drawing.Point(7, 53);
  275.             this.loadSyncButton.Name = "loadSyncButton";
  276.             this.loadSyncButton.Size = new System.Drawing.Size(142, 23);
  277.             this.loadSyncButton.TabIndex = 4;
  278.             this.loadSyncButton.Text = "Load Synchronously";
  279.             this.loadSyncButton.Click += new System.EventHandler(this.loadSyncButton_Click);
  280.             //
  281.             // playOnceSyncButton
  282.             //
  283.             this.playOnceSyncButton.Location = new System.Drawing.Point(7, 86);
  284.             this.playOnceSyncButton.Name = "playOnceSyncButton";
  285.             this.playOnceSyncButton.Size = new System.Drawing.Size(142, 23);
  286.             this.playOnceSyncButton.TabIndex = 5;
  287.             this.playOnceSyncButton.Text = "Play Once Synchronously";
  288.             this.playOnceSyncButton.Click += new System.EventHandler(this.playOnceSyncButton_Click);
  289.             //
  290.             // playOnceAsyncButton
  291.             //
  292.             this.playOnceAsyncButton.Location = new System.Drawing.Point(149, 86);
  293.             this.playOnceAsyncButton.Name = "playOnceAsyncButton";
  294.             this.playOnceAsyncButton.Size = new System.Drawing.Size(147, 23);
  295.             this.playOnceAsyncButton.TabIndex = 6;
  296.             this.playOnceAsyncButton.Text = "Play Once Asynchronously";
  297.             this.playOnceAsyncButton.Click += new System.EventHandler(this.playOnceAsyncButton_Click);
  298.             //
  299.             // stopButton
  300.             //
  301.             this.stopButton.Location = new System.Drawing.Point(149, 109);
  302.             this.stopButton.Name = "stopButton";
  303.             this.stopButton.Size = new System.Drawing.Size(147, 23);
  304.             this.stopButton.TabIndex = 7;
  305.             this.stopButton.Text = "Stop";
  306.             this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
  307.             //
  308.             // playLoopAsyncButton
  309.             //
  310.             this.playLoopAsyncButton.Location = new System.Drawing.Point(7, 109);
  311.             this.playLoopAsyncButton.Name = "playLoopAsyncButton";
  312.             this.playLoopAsyncButton.Size = new System.Drawing.Size(142, 23);
  313.             this.playLoopAsyncButton.TabIndex = 8;
  314.             this.playLoopAsyncButton.Text = "Loop Asynchronously";
  315.             this.playLoopAsyncButton.Click += new System.EventHandler(this.playLoopAsyncButton_Click);
  316.             //
  317.             // statusBar
  318.             //
  319.             this.statusBar.Location = new System.Drawing.Point(0, 146);
  320.             this.statusBar.Name = "statusBar";
  321.             this.statusBar.Size = new System.Drawing.Size(306, 22);
  322.             this.statusBar.SizingGrip = false;
  323.             this.statusBar.TabIndex = 9;
  324.             this.statusBar.Text = "(no status)";
  325.             //
  326.             // loadAsyncButton
  327.             //
  328.             this.loadAsyncButton.Location = new System.Drawing.Point(149, 53);
  329.             this.loadAsyncButton.Name = "loadAsyncButton";
  330.             this.loadAsyncButton.Size = new System.Drawing.Size(147, 23);
  331.             this.loadAsyncButton.TabIndex = 10;
  332.             this.loadAsyncButton.Text = "Load Asynchronously";
  333.             this.loadAsyncButton.Click += new System.EventHandler(this.loadAsyncButton_Click);
  334.             //
  335.             // SoundTestForm
  336.             //
  337.             this.ClientSize = new System.Drawing.Size(306, 168);
  338.             this.Controls.Add(this.loadAsyncButton);
  339.             this.Controls.Add(this.statusBar);
  340.             this.Controls.Add(this.playLoopAsyncButton);
  341.             this.Controls.Add(this.stopButton);
  342.             this.Controls.Add(this.playOnceAsyncButton);
  343.             this.Controls.Add(this.playOnceSyncButton);
  344.             this.Controls.Add(this.loadSyncButton);
  345.             this.Controls.Add(this.label1);
  346.             this.Controls.Add(this.selectFileButton);
  347.             this.Controls.Add(this.filepathTextbox);
  348.             this.MinimumSize = new System.Drawing.Size(310, 165);
  349.             this.Name = "SoundTestForm";
  350.             this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
  351.             this.Text = "Sound API Test Form";
  352.             this.ResumeLayout(false);
  353.  
  354.         }
  355.         #endregion
  356.  
  357.         [STAThread]
  358.         static void Main()
  359.         {
  360.             Application.Run(new SoundTestForm());
  361.         }
  362.     }
  363. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top