简体   繁体   中英

VS2010 C# function using a loop

I am currently implementing my final year project as a Music Software Development student. I am making a software that plays a midi track that is generated earlier in the process.

In my interface, there is a button "play" and a button "stop". When I click "play", a loop is called that plays this track. In this loop, I call a function from an instance of an other class.

What I would like is that when the user press "pause", the loop is temporary stopped, and when he press "pause" again, it goes back exactly where it was in the loop.

So far, when I press play, the track plays exactly how I want to, but I can't use any other element of my window until the loop is not completly processed.

Here is my code.

private void playStopButton_Click(object sender, RoutedEventArgs e)
    {
        // Initialising my output midi devices
        outDevice.Send(new ChannelMessage(ChannelCommand.ProgramChange, information.bassChannel, information.bassSound));//bass
        outDevice.Send(new ChannelMessage(ChannelCommand.ProgramChange, information.guitarChannel, information.guitarSound));//guitar

        for (int barNum = 0; barNum < Globals.numberOfBars; barNum++)
        {
            // playing the first beat
            rythmicPattern.play(0, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //second beat
            rythmicPattern.play(1, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //thrid beat
            rythmicPattern.play(2, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //fourth beat
            rythmicPattern.play(3, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);
        }
    }

private void playPauseButton_Click(object sender, RoutedEventArgs e)
        {
            // test if the track is being played
            if (isPlaying)
                rythmicPattern.Pause();
            else
                // if it was already paused, playing back from where I stopped
                rythmicPattern.PlayAfterPause(beatNumber);
        }

Thank you for your help. Greg

您必须使用线程,您可以在另一个线程中播放声音,因此您可以执行暂停方法检查此链接.NET中的多线程:简介和建议

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM