ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Windows Form에서 비동기적으로 소리 로드 System.Media; SoundPlayer
    .NET 2009. 3. 10. 20:36
    반응형
    Windows Forms 프로그래밍
    방법: Windows Form에서 비동기적으로 소리 로드

    업데이트: 2007년 11월

    다음 코드 예제에서는 URL에서 소리를 비동기적으로 로드한 다음 새 스레드에서 재생합니다.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Media;
    using System.Windows.Forms;
    
    namespace SoundPlayerLoadAsyncExample
    {
        public class Form1 : Form
        {
            private SoundPlayer Player = new SoundPlayer();
    
            public Form1()
            {
                InitializeComponent();
    
                this.Player.LoadCompleted += new AsyncCompletedEventHandler(Player_LoadCompleted);
            }
    
            private void playSoundButton_Click(object sender, EventArgs e)
            {
                this.LoadAsyncSound();
            }
    
            public void LoadAsyncSound()
            {
                try
                {
                    // Replace this file name with a valid file name.
                    this.Player.SoundLocation = "http://www.tailspintoys.com/sounds/stop.wav";
                    this.Player.LoadAsync();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error loading sound");
                }
            }
    
            // This is the event handler for the LoadCompleted event.
            void Player_LoadCompleted(object sender, AsyncCompletedEventArgs e)
            {
                if (Player.IsLoadCompleted)
                {
                    try
                    {
                        this.Player.Play();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error playing sound");
                    }
                }
            }
    
            private Button playSoundButton;
    
            /// <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 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.playSoundButton = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // playSoundButton
                // 
                this.playSoundButton.Location = new System.Drawing.Point(106, 112);
                this.playSoundButton.Name = "playSoundButton";
                this.playSoundButton.Size = new System.Drawing.Size(75, 23);
                this.playSoundButton.TabIndex = 0;
                this.playSoundButton.Text = "Play Sound";
                this.playSoundButton.Click += new System.EventHandler(this.playSoundButton_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.Add(this.playSoundButton);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
        }
    
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.Run(new Form1());
            }
        }
    }
    
    

    이 예제에는 다음 사항이 필요합니다.

    • System 및 System.Windows.Forms 어셈블리에 대한 참조

    • 파일 이름 "http://www.tailspintoys.com/sounds/stop.wav"를 올바른 파일 이름으로 바꿈

    Visual Basic 또는 Visual C#의 명령줄에서 이 예제를 빌드하는 방법에 대한 자세한 내용은 명령줄에서 빌드(Visual Basic) 또는 csc.exe를 사용한 명령줄 빌드를 참조하십시오. Visual Studio에서 코드를 새 프로젝트에 붙여넣어 이 예제를 빌드할 수도 있습니다. 자세한 내용은 다음을 참조하십시오. 방법: Visual Studio를 사용하여 전체 Windows Forms 코드 예제 컴파일 및 실행.

    파일 작업은 적절한 예외 처리 블록 내에 있어야 합니다.

    다음 조건에서 예외가 발생할 수 있습니다.

    파일 이름을 바탕으로 파일 내용을 판단하면 안 됩니다. 예를 들어, Form1.vb 파일이 Visual Basic 소스 파일이 아닐 수도 있습니다. 응용 프로그램에서 데이터를 사용하기 전에 모든 입력을 확인해야 합니다.

    반응형

    댓글

Designed by Tistory.