逐个播放mp3音频文件会冻结表单 [英] Playing mp3 audio files one by one freezes the form

查看:121
本文介绍了逐个播放mp3音频文件会冻结表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在C#中开展一个项目(2015年视觉工作室)。

我有问题。当我逐个播放许多音频文件时,表单会冻结,我无法在此表单上执行其他任务,例如从组合框中选择其他选项,甚至在拖动鼠标时移动表单。

我的音频文件持续时间短,平均10到20秒。表单在播放音频时冻结。在它开始之前,下一个音频形式解冻。但它只有一秒钟然后再次冻结,因为它正在播放下一个音频。



这是我的代码:



我尝试了什么:



Hi I am working on a project in C# (visual studio 2015).
I have a problem. when I play many audio files one by one, the form freezes and I cannot do another task on this form, like selecting another option from combo box or even move the form while mouse dragging.
My audio files are short in duration like 10 to 20 seconds average. The form freezes while it is playing audio. Before it start the next audio form unfreezes. but it is for hardly one second and then again freezes because it is playing the next audio.

Here is my code:

What I have tried:

<pre>
        private void cmbVerse_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = listView1.Items.IndexOf(listView1.SelectedItems[0]);
            int surah = listView1.Items.IndexOf(listView1.SelectedItems[0]) + 1;

            //Load Surah Text file here. i.e. 001.txt
            lines = File.ReadAllLines(@"C:\Users\Sajid Qureshi\Documents\Visual Studio 2015\Projects\First\First\bin\Debug\Resources\" + surah.ToString("000") + ".txt");

            currentIndex = cmbVerse.SelectedIndex;

            richTextBox1.Text = lines[currentIndex];
            mPlayer.URL = Application.StartupPath + "\\Ayaat\\"+(comboBox1.Text).Substring(0,3) + (Int32.Parse(cmbVerse.Text)).ToString("000") + ".mp3";
            mPlayer.Ctlcontrols.play();
            Application.DoEvents();
            Thread.Sleep(TimeSpan.FromSeconds(mPlayer.currentMedia.duration));
            mPlayer.Ctlcontrols.stop();

            if (cmbVerse.SelectedIndex < cmbVerse.Items.Count-1 )
                cmbVerse.SelectedIndex += 1;

        }







我认为这条线造成了问题。 />




I think this line is causing problem.

Thread.Sleep(TimeSpan.FromSeconds(mPlayer.currentMedia.duration));



但没有这个我无法播放音频一个接一个。

请帮助


but without this I cannot Play audio one by one.
Please help

推荐答案

永远不要打电话给 Sleep() in您的主线程(GUI线程),因为它会阻止执行,因为您已经注意到了。在工作线程中也避免使用它。使用它表示设计不良。



解决方案是使用工作线程(在使用媒体播放器时已经在内部使用)和处理事件。 />


在您的情况下,您可以使用 AxWindowsMediaPlayer.playState属性(Windows) [ ^ ]获取实际状态(例如,检查实际播放的声音是否应该在播放新声音之前停止)和< a href =https://msdn.microsoft.com/en-us/library/windows/desktop/dd562460(v=vs.85).aspx> AxWindowsMediaPlayer对象的PlayStateChange事件(Windows) [ ^ ]在比赛结束时获得通知(知道什么时候开始)可以启动另一种媒体)。



如何以及何时使用这些和其他功能取决于你想做什么,什么时候应该做,以及哪种应该处理事件。首先定义它并考虑如何实现它。这是一项设计任务。然后阅读已使用类的文档(此处为Media Player和相关类)。完成后,通过编写代码开始实现它。
Never call Sleep() in your main thread (the GUI thread) because it blocks execution as you have noticed. Avoid it also in worker threads. Using it is an indication of bad design.

The solution is to use worker threads (which are already used internally when using the Media Player) and handling events.

In your case you might use the AxWindowsMediaPlayer.playState property (Windows)[^] to get the actuals state (e.g. to check if an actually playing sound should be stopped before playing a new one) and the PlayStateChange Event of the AxWindowsMediaPlayer Object (Windows)[^] to get informed when playing has stopped at the end (to know when playing another media can be started).

How and when to use these and other functions depends on what you want to do, when it should be done, and which kinds of events should be handled. Define this first and think about how it can be implemented. That is a design task. Then read the documentation for the used classes (here the Media Player and the related classes). Once that is done, start implementing it by writing code.


       private void cmbVerse_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = listView1.Items.IndexOf(listView1.SelectedItems[0]);
            int surah = listView1.Items.IndexOf(listView1.SelectedItems[0]) + 1;

            //Load Surah Text file here. i.e. 001.txt
            lines = File.ReadAllLines(@"C:\Users\Sajid Qureshi\Documents\Visual Studio 2015\Projects\First\First\bin\Debug\Resources\" + surah.ToString("000") + ".txt");
            label7.Text = lines.Count().ToString();

            currentIndex = cmbVerse.SelectedIndex;
            label7.Text = (comboBox1.Text).Substring(0, 3);
            richTextBox1.Text = lines[currentIndex];
            mPlayer.URL = Application.StartupPath + "\\Ayaat\\"+(comboBox1.Text).Substring(0,3) + (Int32.Parse(cmbVerse.Text)).ToString("000") + ".mp3";
            mPlayer.Ctlcontrols.play();
        }

<pre>        private void mPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            // Test the current state of the player and display a message for each state.
            switch (e.newState)
            {
                case 0:    // Undefined
                    currentStateLabel.Text = "Undefined";
                    break;

                case 1:    // Stopped
                    currentStateLabel.Text = "Stopped";
                    if (cmbVerse.SelectedIndex < cmbVerse.Items.Count - 1)
                        cmbVerse.SelectedIndex += 1;
                    break;

                case 2:    // Paused
                :
                :
                :
        }


这篇关于逐个播放mp3音频文件会冻结表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆