c# - C# 设置歌曲时长的方法

标签 c# web-applications soundplayer

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

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

    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 类。

最佳答案

也许我没有正确理解你的意图,但为什么你甚至使用线程来计时(而且我猜 StopSound() 不是合适的调用方法)?为什么不只是:

...
player.Play();
Thread.Sleep(musicExecuteTime * 1000);
player.Stop();
...

关于c# - C# 设置歌曲时长的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16775521/

相关文章:

java - Spring Boot 执行器 - "/shutdown"失败并出现 500 错误

java - 在 Web 应用程序中运行 ScheduledExecutorService 是否有任何影响

c# - 如何在指定的时间内播放音乐文件

c# - 在 C# 中以距开始时间偏移的时间播放 WAV 文件中的声音

java - SoundPool 只停止一次

c# - 所有数学运算都以整数形式返回?

c# - Linq-to-SQL:忽略 WHERE 子句中的空参数

c# - 奇怪的 .Where() 行为。有人有解释吗?

c# - 使用 Azure 表可以实现这一点吗?

java - 如何在 Java Web 应用程序中使用 wkhtmltopdf?