[英]Playing sounds on Console - C#
I'm writing a Console application on C# and I want to play a sound when I display texts continuously. 我正在C#上编写一个Console应用程序,我希望在连续显示文本时播放声音。 This is what I've done :
这就是我所做的:
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
is imported to my project next to the .cs
files and I've selected copy always
. typewriter.wav
导入到.cs
文件旁边的项目中,我copy always
选择copy always
。 When I run this code, an error pops out when starting playing the sound saying Please be sure a sound file exists at the specified location.
当我运行此代码时,在开始播放声音时弹出错误说明
Please be sure a sound file exists at the specified location.
What is wrong here? 这有什么不对?
EDIT : Changed my code to the following according to Kevin J's answer. 编辑:根据Kevin J的回答将我的代码更改为以下内容。
static SoundPlayer typewritter;
public static void Load()
{
Assembly assembly;
assembly = Assembly.GetExecutingAssembly();
typewritter = new SoundPlayer(assembly.GetManifestResourceStream
("typewriter"));
}
I also should precised to use the path Environment.CurruntDirectory + "typewriter"
but nothing changes. 我也应该使用路径
Environment.CurruntDirectory + "typewriter"
但没有任何改变。
Figured out the problem : I just needed to set the SoundLocation
property of the SoundPlayer
instance : 找出问题:我只需要设置
SoundPlayer
实例的SoundLocation
属性:
SoundPlayer typewriter = new SoundPlayer();
typewriter.SoundLocation = Environment.CurrentDirectory + "/typewriter.wav";
Here's something that might help you out (please note that this code is for a winforms app, but you should be able to convert to a console app. Just study the code to see how it works) You'll basically be adding the .wav file as a 'resource' to your program. 这里有些东西可以帮助你(请注意这个代码适用于winforms应用程序,但你应该能够转换为控制台应用程序。只需研究代码以了解它是如何工作的)你基本上会添加.wav将文件作为程序的“资源”。 Then, your program can access the .wav file and play it:
然后,您的程序可以访问.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();
}
}
}
If for example you have your sounds in the folder "Assets" then subfolder "SoundClips" do it like this. 例如,如果您在“资产”文件夹中有声音,那么子文件夹“SoundClips”会像这样做。
var soundLocation = Environment.CurrentDirectory + @"\Assets\SoundClips\";
SoundPlayer player = new SoundPlayer
{
SoundLocation = soundLocation + "typewriter.wav",
};
Make sure you have the file properties set to: 确保将文件属性设置为:
build action - Content 构建动作 - 内容
copy to output directory - Copy if newer 复制到输出目录 - 如果更新则复制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.