| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1121 人关注过本帖
标题:在 Windows 窗体内异步加载声音
只看楼主 加入收藏
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
结帖率:80.91%
收藏
 问题点数:0 回复次数:1 
在 Windows 窗体内异步加载声音
using System;
using System.Collections.Generic;
using
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.
                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 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());
        }
    }
}
搜索更多相关主题的帖子: 加载 异步 窗体 Windows 声音 
2008-06-06 10:41
guang
Rank: 4
来 自:广东深圳
等 级:贵宾
威 望:13
帖 子:1414
专家分:285
注 册:2006-4-3
收藏
得分:0 
你不会还在用2003吧

不相信未作牺牲竟先可拥有,只相信靠双手找到我的欲求!!
我的博客:http://liao5930.blog.
2008-06-06 11:24
快速回复:在 Windows 窗体内异步加载声音
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015495 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved