using System; using System.Collections; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Media; using System.Windows.Forms; namespace SoundApiExample ...{ publicclass SoundTestForm : System.Windows.Forms.Form ...{ private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox filepathTextbox; private System.Windows.Forms.Button playOnceSyncButton; private System.Windows.Forms.Button playOnceAsyncButton; private System.Windows.Forms.Button playLoopAsyncButton; private System.Windows.Forms.Button selectFileButton; private System.Windows.Forms.Button stopButton; private System.Windows.Forms.StatusBar statusBar; private System.Windows.Forms.Button loadSyncButton; private System.Windows.Forms.Button loadAsyncButton; private SoundPlayer player; public SoundTestForm() ...{ // Initialize Forms Designer generated code. InitializeComponent(); // Disable playback controls until a valid .wav file // is selected. EnablePlaybackControls(false); // Set up the status bar and other controls. InitializeControls(); // Set up the SoundPlayer object. InitializeSound(); } // Sets up the status bar and other controls. privatevoid InitializeControls() ...{ // Set up the status bar. StatusBarPanel panel =new StatusBarPanel(); panel.BorderStyle = StatusBarPanelBorderStyle.Sunken; panel.Text ="Ready."; panel.AutoSize = StatusBarPanelAutoSize.Spring; this.statusBar.ShowPanels =true; this.statusBar.Panels.Add(panel); } // Sets up the SoundPlayer object. privatevoid InitializeSound() ...{ // Create an instance of the SoundPlayer class. player =new SoundPlayer(); // Listen for the LoadCompleted event. player.LoadCompleted +=new AsyncCompletedEventHandler(player_LoadCompleted); // Listen for the SoundLocationChanged event. player.SoundLocationChanged +=new EventHandler(player_LocationChanged); } privatevoid selectFileButton_Click(object sender, System.EventArgs e) ...{ // Create a new OpenFileDialog. OpenFileDialog dlg =new OpenFileDialog(); // Make sure the dialog checks for existence of the // selected file. dlg.CheckFileExists =true; // Allow selection of .wav files only. dlg.Filter ="WAV files (*.wav)|*.wav"; dlg.DefaultExt =".wav"; // Activate the file selection dialog. if (dlg.ShowDialog() == DialogResult.OK) ...{ // Get the selected file's path from the dialog. this.filepathTextbox.Text = dlg.FileName; // Assign the selected file's path to // the SoundPlayer object. player.SoundLocation = filepathTextbox.Text; } } // Convenience method for setting message text in // the status bar. privatevoid ReportStatus(string statusMessage) ...{ // If the caller passed in a message... if ((statusMessage !=null) && (statusMessage != String.Empty)) ...{ // ...post the caller's message to the status bar. this.statusBar.Panels[0].Text = statusMessage; } } // Enables and disables play controls. privatevoid EnablePlaybackControls(bool enabled) ...{ this.playOnceSyncButton.Enabled = enabled; this.playOnceAsyncButton.Enabled = enabled; this.playLoopAsyncButton.Enabled = enabled; this.stopButton.Enabled = enabled; } privatevoid filepathTextbox_TextChanged(object sender, EventArgs e) ...{ // Disable playback controls until the new .wav is loaded. EnablePlaybackControls(false); } privatevoid loadSyncButton_Click(object sender, System.EventArgs e) ...{ // Disable playback controls until the .wav is // successfully loaded. The LoadCompleted event // handler will enable them. EnablePlaybackControls(false); try ...{ // Assign the selected file's path to // the SoundPlayer object. player.SoundLocation = filepathTextbox.Text; // Load the .wav file. player.Load(); } catch (Exception ex) ...{ ReportStatus(ex.Message); } } privatevoid loadAsyncButton_Click(System.Object sender, System.EventArgs e) ...{ // Disable playback controls until the .wav is // successfully loaded. The LoadCompleted event // handler will enable them. EnablePlaybackControls(false); try ...{ // Assign the selected file's path to // the SoundPlayer object. player.SoundLocation =this.filepathTextbox.Text; // Load the .wav file. player.LoadAsync(); } catch (Exception ex) ...{ ReportStatus(ex.Message); } } // Synchronously plays the selected .wav file once. // If the file is large, UI response will be visibly // affected. privatevoid playOnceSyncButton_Click(object sender, System.EventArgs e) ...{ ReportStatus("Playing .wav file synchronously."); player.PlaySync(); ReportStatus("Finished playing .wav file synchronously."); } // Asynchronously plays the selected .wav file once. privatevoid playOnceAsyncButton_Click(object sender, System.EventArgs e) ...{ ReportStatus("Playing .wav file asynchronously."); player.Play(); } // Asynchronously plays the selected .wav file until the user // clicks the Stop button. privatevoid playLoopAsyncButton_Click(object sender, System.EventArgs e) ...{ ReportStatus("Looping .wav file asynchronously."); player.PlayLooping(); } // Stops the currently playing .wav file, if any. privatevoid stopButton_Click(System.Object sender, System.EventArgs e) ...{ player.Stop(); ReportStatus("Stopped by user."); } // Handler for the LoadCompleted event. privatevoid player_LoadCompleted(object sender, AsyncCompletedEventArgs e) ...{ string message = String.Format("LoadCompleted: {0}", this.filepathTextbox.Text); ReportStatus(message); EnablePlaybackControls(true); } // Handler for the SoundLocationChanged event. privatevoid player_LocationChanged(object sender, EventArgs e) ...{ string message = String.Format("SoundLocationChanged: {0}", player.SoundLocation); ReportStatus(message); }
using System;using System.Collections;using System.ComponentModel;using System.Diagnostics;using System.Drawing;using System.Media;using System.Windows.Forms;namespace SoundApiExample...{ public