Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
<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
Posted
Updated 12-Jun-18 23:40pm
Comments
F-ES Sitecore 13-Jun-18 4:41am    
You're going to need some way of knowing when the audio has stopped playing like some kind of end or stop event. Use that event to start playing the next bit of audio.

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.
 
Share this answer
 
Comments
Sajid_Qureshi 13-Jun-18 5:40am    
Thank you very much. It worked. I have changed my code accordingly:
Jochen Arndt 13-Jun-18 5:45am    
Fine that you have solved it and thank your accepting my solution.
       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
                :
                :
                :
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900