尔冬橙

博客园 首页 新随笔 联系 订阅 管理

最近写一个程序,声音的播放需要放入一个线程中播放,当声音播放开始和完毕之后需要执行事件处理函数,可是如果我把声音播放的事件绑定放入线程执行的方法中。

事件方法却得不到执行。后来在MSDN论坛的帮助下。把事件的绑定放到线程外边。线程只执行播放操作。这个问题就解决了。

一开始有问题的代码:

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

using System.Media;
using System.IO;

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Audio audio;      
        
        public Form1()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Thread playThread = new Thread(new ThreadStart(play));
            playThread.Priority = ThreadPriority.Highest;            
            playThread.Start();

            //Audio audio = new Audio("hello.mp3");
            //audio.Starting += new EventHandler(audio_Starting);
            //audio.Ending += new EventHandler(audio_Ending);
            //audio.Play();
        }
        void audio_Starting(object sender, EventArgs e)
        {
            MessageBox.Show("开始播放");
        }
        void audio_Ending(object sender, EventArgs e)
        {
            MessageBox.Show("播放完成");
        }
        void play()
        {
            audio = new Audio("hello.mp3");
            audio.Starting += new EventHandler(audio_Starting);
            audio.Ending += new EventHandler(audio_Ending);
            audio.Play();
        }
    }
}

改进过的代码:

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

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;
using System.Threading;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        Audio audio;
        public Form1()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                audio = new Audio("hello.mp3");
                audio.Starting += new EventHandler(audio_Starting);
                audio.Ending += new EventHandler(audio_Ending);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            Thread playThread = new Thread(new ThreadStart(play));
            playThread.Priority = ThreadPriority.Highest;
            playThread.IsBackground = true;
            playThread.Start();
        }
        void audio_Starting(object sender, EventArgs e)
        {
            MessageBox.Show("开始播放");
        }
        void audio_Ending(object sender, EventArgs e)
        {
            MessageBox.Show("播放完成");
        }
        void play()
        {
            audio.Play();
        }

    }
}

 

posted on 2012-10-09 12:35  尔冬橙  阅读(1738)  评论(0编辑  收藏  举报