2

作为选择的结果,我有一个应用程序可以在指定的时间内播放一首特定的歌曲。

我已经可以播放歌曲,但我无法设置持续时间。

    public void PlaySound()
    {
        int i = 0;

        foreach (string musicFile in musicFiles)
        {
            Thread thrStopMusic = new Thread(ThreadTimer);
            player.SoundLocation = musicFile;
            musicExecuteTime = GetMusicDuration[i];
            player.Play();
            thrStopMusic.Start();
            thrStopMusic.Abort();
            i++;
        }
    }

 public void ThreadTimer()
    {
       Thread.Sleep(musicExecuteTime * 1000);
       StopSound();
    }

我正在使用 SoundPlayer 类。

4

2 回答 2

2

也许我没有正确理解您的意图,但是您为什么还要使用线程来计时(我想StopSound()这不是合适的调用方法)?为什么不只是:

...
player.Play();
Thread.Sleep(musicExecuteTime * 1000);
player.Stop();
...
于 2013-05-27T14:51:59.600 回答
1

我认为你可以做这样的事情。Play()使用一个新线程来播放文件,所以你应该只需要在调用之前“暂停”你的线程一段时间Stop()

public void PlaySound()
{
    int i = 0;

    foreach (string musicFile in musicFiles)
    {
        player.SoundLocation = musicFile;
        player.Play();
        Thread.Sleep(1000 * GetMusicDuration[i])
        player.Stop();
        i++;
    }
}
于 2013-05-27T14:53:02.533 回答