【问题标题】:Playing sounds on Console - C#在控制台上播放声音 - C#
【发布时间】:2014-03-28 12:45:17
【问题描述】:

我正在用 C# 编写控制台应用程序,我想在连续显示文本时播放声音。这就是我所做的:

static SoundPlayer typewriter = new SoundPlayer("typewriter");
static public void print(string str, int delay)
    {
        Thread skipThread = new Thread(skipText);
        typewriter.PlayLooping();
        textgap = delay;
        foreach (char c in str)
        {
            Console.Write(c);
            if (textgap != 0)
                Thread.Sleep(textgap);

        }
        typewriter.Stop();

    }

typewriter.wav 被导入到我的项目中,位于.cs 文件旁边,并且我选择了copy always。当我运行此代码时,开始播放声音时会弹出一个错误说Please be sure a sound file exists at the specified location. 这里有什么问题?

编辑: 根据 Kevin J 的回答将我的代码更改为以下内容。

static SoundPlayer typewritter;

    public static void Load()
    {
        Assembly assembly;
        assembly = Assembly.GetExecutingAssembly();
        typewritter = new SoundPlayer(assembly.GetManifestResourceStream
            ("typewriter"));
    }

我也应该精确使用路径Environment.CurruntDirectory + "typewriter",但没有任何变化。

【问题讨论】:

  • 它是否将文件与可执行文件放在同一文件夹中?
  • @DStanley 是的,确实如此。
  • 除非您已将 .wav 文件作为资源导入,否则您需要为 SoundPlayer 指定 .wav 文件的确切路径。即SoundPlayer player = new SoundPlayer("C:\\bass.wav"))。您上面的代码似乎没有完整路径。
  • 查看您的新代码,您没有为assembly.GetManifestResourceStream 放置正确的资源标识符。它应该类似于“Yournamespace.typewriter.wav”。你只有“打字机”。您必须使用应用程序的命名空间和正确的文件名来限定资源。 “打字机”不是 .wav 文件的正确名称。
  • @KevinJ 我不能将“mynamespace.typewriter.wav”作为函数的参数,因为它说没有定义打字机。

标签: c# audio console console-application soundplayer


【解决方案1】:

例如,如果您的声音在文件夹“Assets”中,那么子文件夹“SoundClips”就是这样。

var soundLocation = Environment.CurrentDirectory + @"\Assets\SoundClips\";

SoundPlayer player = new SoundPlayer
{
    SoundLocation = soundLocation + "typewriter.wav",
};

确保您将文件属性设置为:

构建动作 - 内容

复制到输出目录 - 如果较新则复制

【讨论】:

    【解决方案2】:

    解决了问题:我只需要设置SoundPlayer 实例的SoundLocation 属性:

    SoundPlayer typewriter = new SoundPlayer();
    typewriter.SoundLocation = Environment.CurrentDirectory + "/typewriter.wav";
    

    【讨论】:

      【解决方案3】:

      这里有一些可能对您有所帮助(请注意,此代码适用于 winforms 应用程序,但您应该能够转换为控制台应用程序。只需研究代码以了解它是如何工作的)您基本上会添加.wav 文件作为程序的“资源”。然后,您的程序可以访问 .wav 文件并播放它:

      using System.Reflection;
      using System.IO;
      using System.Resources;
      using System.Media;
      using System.Diagnostics;
      
      
      
      namespace Yournamespace
      {
          public partial class Form2 : Form
          {
              public Form2()
              {
                  InitializeComponent();
              }
      
              private void Form2_Load(object sender, EventArgs e)
              {
                  Assembly assembly;
                  Stream soundStream;
                  SoundPlayer sp;
                  assembly = Assembly.GetExecutingAssembly();
                  sp = new SoundPlayer(assembly.GetManifestResourceStream
                      ("Yournamespace.Dreamer.wav"));
                  sp.Play();  
              } 
          }
      }
      

      【讨论】:

      • 谢谢你的回答,当我这个时,至少没有错误但播放的声音只是系统哔声,与我的原声无关。
      • 在原始问题中发布您的新代码作为编辑,让我们看看您有什么。
      • 根据你的cmets编辑了我的帖子。
      猜你喜欢
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      相关资源
      最近更新 更多