Noice Maker : Sound « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » SoundScreenshots 
Noice Maker
Noice Maker

/*
Code revised from chapter 1


GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward 

Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604

Sample chapter
http://www.packtpub.com/files/1604_CustomControls_SampleChapter.pdf


For More info on GDI+ Custom Control with Microsoft Visual C# book 
visit website www.packtpub.com 


*/ 



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.IO;

    public partial class Form1 : Form
    {
        private TinyNoiseMaker tinyNoiseMaker1;
        private System.Windows.Forms.Button playButton;

        private System.ComponentModel.IContainer components = null;

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.playButton = new System.Windows.Forms.Button();
            this.tinyNoiseMaker1 = new TinyNoiseMaker();
            this.SuspendLayout();
            // 
            // playButton
            // 
            this.playButton.Location = new System.Drawing.Point(10022);
            this.playButton.Name = "playButton";
            this.playButton.Size = new System.Drawing.Size(8223);
            this.playButton.TabIndex = 1;
            this.playButton.Text = "Play My File";
            this.playButton.UseVisualStyleBackColor = true;
            this.playButton.Click += new System.EventHandler(this.playButton_Click);
            // 
            // tinyNoiseMaker1
            // 
            this.tinyNoiseMaker1.BackColor = System.Drawing.SystemColors.ControlLight;
            this.tinyNoiseMaker1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.tinyNoiseMaker1.FileName = null;
            this.tinyNoiseMaker1.Location = new System.Drawing.Point(2168);
            this.tinyNoiseMaker1.Name = "tinyNoiseMaker1";
            this.tinyNoiseMaker1.Size = new System.Drawing.Size(26837);
            this.tinyNoiseMaker1.TabIndex = 0;
            this.tinyNoiseMaker1.PlayStart += new System.EventHandler(this.tinyNoiseMaker1_PlayStart);
            this.tinyNoiseMaker1.PlayStop += new System.EventHandler(this.tinyNoiseMaker1_PlayStop);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(316163);
            this.Controls.Add(this.playButton);
            this.Controls.Add(this.tinyNoiseMaker1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private void tinyNoiseMaker1_PlayStart(object sender, EventArgs e)
        {
            Text = "Started Play of: " + tinyNoiseMaker1.FileName;
        }

        private void tinyNoiseMaker1_PlayStop(object sender, EventArgs e)
        {
            Text = "Stopped Play of : " + tinyNoiseMaker1.FileName;
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            tinyNoiseMaker1.FileName = "C:\\WINDOWS\\Media\\tada.wav";
            tinyNoiseMaker1.Play();
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        
    }
    
    public partial class TinyNoiseMaker : UserControl
    {
        public event EventHandler PlayStart;
        public event EventHandler PlayStop;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.openButton = new System.Windows.Forms.Button();
            this.playButton = new System.Windows.Forms.Button();
            this.stopButton = new System.Windows.Forms.Button();
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            // 
            // openButton
            // 
            this.openButton.Location = new System.Drawing.Point(44);
            this.openButton.Name = "openButton";
            this.openButton.Size = new System.Drawing.Size(8225);
            this.openButton.TabIndex = 0;
            this.openButton.Text = "Open";
            this.openButton.UseVisualStyleBackColor = true;
            this.openButton.Click += new System.EventHandler(this.openButton_Click);
            // 
            // playButton
            // 
            this.playButton.Location = new System.Drawing.Point(864);
            this.playButton.Name = "playButton";
            this.playButton.Size = new System.Drawing.Size(8225);
            this.playButton.TabIndex = 1;
            this.playButton.Text = "Play";
            this.playButton.UseVisualStyleBackColor = true;
            this.playButton.Click += new System.EventHandler(this.playButton_Click);
            // 
            // stopButton
            // 
            this.stopButton.Location = new System.Drawing.Point(1684);
            this.stopButton.Name = "stopButton";
            this.stopButton.Size = new System.Drawing.Size(8225);
            this.stopButton.TabIndex = 2;
            this.stopButton.Text = "Stop";
            this.stopButton.UseVisualStyleBackColor = true;
            this.stopButton.Click += new System.EventHandler(this.stopButton_Click);
            // 
            // openFileDialog
            // 
            this.openFileDialog.FileName = "openFileDialog";
            // 
            // TinyNoiseMaker
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ControlLight;
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.Controls.Add(this.stopButton);
            this.Controls.Add(this.playButton);
            this.Controls.Add(this.openButton);
            this.Name = "TinyNoiseMaker";
            this.Size = new System.Drawing.Size(25030);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button openButton;
        private System.Windows.Forms.Button playButton;
        private System.Windows.Forms.Button stopButton;
        private System.Windows.Forms.OpenFileDialog openFileDialog;

        protected virtual void OnPlayStart(EventArgs e)
        {
            if (PlayStart != null)
            {
                PlayStart(this, e);
            }
        }

        protected virtual void OnPlayStop(EventArgs e)
        {
            if (PlayStop != null)
            {
                PlayStop(this, e);
            }
        }
        
        
        SoundPlayer soundPlayer;
        public TinyNoiseMaker()
        {
            InitializeComponent();
            soundPlayer = new SoundPlayer();
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileName = openFileDialog.FileName;
            }
                
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            Play();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            Stop();
        }
        public void Play()
        {
            soundPlayer.Play();
            OnPlayStart(EventArgs.Empty);
        }
        public void Stop()
        {
            soundPlayer.Stop();
            OnPlayStop(EventArgs.Empty);
        }
        private string fileName;
        public string FileName
        {
            get
            {
                return fileName;
            }
            set
            {
                if (fileName != value)
                {
                    fileName = value;
                    soundPlayer.Stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                }
            }
        }
    }


           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.