C#演奏音乐

C#演奏音乐,c#,audio-player,wmplib,C#,Audio Player,Wmplib,我用Windows窗体应用程序创建了一个简单的纸牌游戏。我唯一需要做的就是添加音乐效果。我用mp3录制了一些声音(画卡等),并通过WMPlib将其添加到游戏中,除一件事外,其他一切都正常工作。 我想在一个方法的中间演奏音乐,而不是在结束之后——我的意思是:< /P> private void Button_Click (object sender, EventArgs e) { //code of player 1 player.URL = @"draw a card.

我用Windows窗体应用程序创建了一个简单的纸牌游戏。我唯一需要做的就是添加音乐效果。我用mp3录制了一些声音(画卡等),并通过WMPlib将其添加到游戏中,除一件事外,其他一切都正常工作。 我想在一个方法的中间演奏音乐,而不是在结束之后——我的意思是:< /P>
private void Button_Click (object sender, EventArgs e)
{
    //code of player 1
    player.URL = @"draw a card.mp3";
    //Immediatelly after that will play player 2
    Player2();
}

void Player2()
{
    //do stuff
    System.Threading.Thread.Sleep(1000);
    //do another stuff
    player.URL = @"draw a card 2.mp3";
}
代码结束后,两种声音一起播放。是否有可能在调用第二个方法之前以某种方式管理它以播放第一个声音? 非常感谢您的帮助;)

试试这个:)


另外,我建议你避开
Thread.Sleep(XXX)
,因为它会暂停执行线程。在你睡觉的时候,不会发生其他任何事情。

如果你睡的是一根线,你就做错了。如果要引入延迟,请使用
wait Task.delay
或创建计时器并处理计时器事件。
private void Button_Click(object sender, EventArgs e)
{
    //code of player 1

    Task.Run(async () => { 
        //this will run the audio and will not wait for audio to end.
        player.URL = @"draw a card.mp3";
    });

    //excecution flow is not interrupted by audio playing so it reaches this line below.
    Player2();
}