C#/.NET Multi-threaded Task, Playing multiple WAV files with WindowsMediaPlay ERROR

0

Hi guys I'm working on a species counterpoint generator using Genetic Algorithms, I've used windows media play to play individual melody but I use a multithreaded solution for playing 2 melodies at same time (PlayMelodies()) but I get this error message (below) any help would be realy appreciated

EDIT: It play 4 notes from both melodies together and then it crashes.

{"The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))"} System.Runtime.InteropServices.COMException

    static Note[] cantus1;
    static Note[] counter1;

    internal void PlayMelody(Note[] melody)
    {

        for (int i = 0; i < melody.Length; i++)
        {
            string s = "H:\\Notes\\" + melody[i].NoteName + ".wav";
            WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
            wmp.URL = s;
            try
            {
                wmp.controls.play();
            }
            catch (COMException e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                wmp.controls.play();
            }

            // Do something.
            for(int i2 = 0; i2 < 100; i2++)
            {
                Thread.Sleep(20);
            }

            wmp.controls.stop();

            /*SoundPlayer player = new SoundPlayer(s);
            Stopwatch stopwatch = new Stopwatch();
            player.Play();
            stopwatch.Start();

            // Do something.
            for(int i2 = 0; i2 < 1000 ; i2++)
            {
                Thread.Sleep(1);
            }

            // Stop timing.
            stopwatch.Stop();
            player.Stop();

            Console.WriteLine(melody[i].NoteName + "\tTime elapsed: {0}", stopwatch.Elapsed);*/

        }
    }

    internal void PlayMelodies(Note[] cantus, Note[] counter)
    {
        cantus1 = cantus;
        counter1 = counter;
        Task.Factory.StartNew(PlayCF);
        Task.Factory.StartNew(PlayCP);
    }

    private void PlayCF()
    {
        PlayMelody(cantus1);
    }

    private void PlayCP()
    {
        PlayMelody(counter1);
    }
}

}

c#
.net
multithreading
genetic-algorithm
multitasking
asked on Stack Overflow Dec 8, 2016 by Joe Allen • edited Dec 8, 2016 by Joe Allen

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0