首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用SoundPlayer播放收藏音乐

用SoundPlayer播放收藏音乐
EN

Stack Overflow用户
提问于 2015-12-11 08:11:03
回答 2查看 374关注 0票数 0

因此,我正在做一架简单的钢琴,并试图遍历我存储笔记的集合,但是SoundPlayer不想在“没有调试模式下”正确地播放它们,只播放最后一个。然而,当我在那里放置断点时,它会播放所有的断点。

代码语言:javascript
复制
public static List<MusicNote> music = new List<MusicNote>(15);
public static void PlayAll()
    {
        SoundPlayer sp = new SoundPlayer();
        for (int i = 0; i <= music.Count - 1; i++)
        {
            string text = music[i].pitch.ToString();
            sp.SoundLocation = (@"c:\my path here\" + text + ".wav");
            sp.Play();
            sp.Stop();
        }
    }

音高是简单的序号链接到文件。

提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-11 08:42:06

你最好使用PlaySyn来告诉你的程序等待音乐完成。

代码语言:javascript
复制
    // Create new SoundPlayer in the using statement.
    using (SoundPlayer player = new SoundPlayer())
    {
      for (int i = 0; i <= music.Count - 1; i++)
           {
               string text = music[i].pitch.ToString();
               sp.SoundLocation = (@"c:\my path here\" + text + ".wav");
               // Use PlaySync to load and then play the sound.
               // ... The program will pause until the sound is complete.
               player.PlaySync();
           }
    }
票数 0
EN

Stack Overflow用户

发布于 2015-12-11 08:33:20

我认为使用PlaySync()而不是Play()更好;

因为这样就不需要停止()方法了。

这里有一个指向SoundPlayer文档的链接

为什么要使用PlaySync?如果您只是在这个程序中调用Play方法,程序将在播放声音之前终止。同步表示在播放声音时,程序应该暂停。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34218802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档