【发布时间】:2020-09-14 15:23:13
【问题描述】:
所以我在 C# 中编写的莫尔斯电码应用程序在声音和字符串返回方面遇到了问题。我正在设置为每个莫尔斯电码字母播放 repsecive wav 文件,同时将用户输入转换为莫尔斯电码,但是虽然每个 .wav 文件都导入到 Visual Studio 项目中,但只有字母 z 的 .wav 文件播放。
另外,当我试图返回翻译后的字符串时,我得到一个空引用异常。这是代码
Dictionary<char, MorseMapping> _morseAlphabetDictionary = new Dictionary<char, MorseMapping>()
{
{ 'a', new MorseMapping(".-", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__A_morse_code.wav") }, //.. here goes the rest
{ 'b', new MorseMapping("-...", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__B_morse_code.wav") }, //.. here goes the rest
{ 'c', new MorseMapping("-.-.", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__C_morse_code.wav") },
//.. here goes the rest
{ 'd', new MorseMapping("-..", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__D_morse_code.wav") },
{ 'e', new MorseMapping(".", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__E_morse_code.wav") },
{ 'f', new MorseMapping("..-.", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__F_morse_code.wav") },
{ 'g', new MorseMapping("--.", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__G_morse_code.wav") },
{ 'h', new MorseMapping("....", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__H_morse_code.wav") },
//.. here goes the rest
{ 'i', new MorseMapping("..", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__I_morse_code.wav") },
//.. here goes the rest
{ 'j', new MorseMapping(".---", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__J_morse_code.wav") },
//.. here goes the rest
{ 'k', new MorseMapping("-.-", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__K_morse_code.wav") },
{ 'l', new MorseMapping(".-..", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__L_morse_code.wav") },
{ 'm', new MorseMapping("--", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__M_morse_code.wav") },
{ 'n', new MorseMapping("-.", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__n_morse_code.wav") },
{ 'o', new MorseMapping("---", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__O_morse_code.wav") },
//.. here goes the rest
{ 'p', new MorseMapping(".--.", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__P_morse_code.wav") },
//.. here goes the rest
{ 'q', new MorseMapping("--.-", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__Q_morse_code.wav") },
//.. here goes the rest
{ 'r', new MorseMapping(".-.", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__R_morse_code.wav") },
{ 's', new MorseMapping("...", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__S_morse_code.wav") },
{ 't', new MorseMapping("-", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__t_morse_code.wav") },
{ 'u', new MorseMapping("..-", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__U_morse_code.wav") },
{ 'v', new MorseMapping("...-", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__V_morse_code.wav") },
//.. here goes the rest
{ 'w', new MorseMapping(".--", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__W_morse_code.wav") },
//.. here goes the rest
{ 'x', new MorseMapping("-..-", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__X_morse_code.wav") },
//.. here goes the rest
{ 'y', new MorseMapping("-.--", @"C:\Users\keife\source\repos\MorseCodeTranslator\MorseCodeTranslator\AnyConv.com__Y_morse_code.wav") },
{ 'z', new MorseMapping("--..", @"C:\Users\keife\Desktop\morse code audio\wav\AnyConv.com__Z_morse_code.wav") },
};
_morseAlphabetDictionary['b'].Player.Play();
_morseAlphabetDictionary['b'].Player.Play();
_morseAlphabetDictionary['c'].Player.Play();
_morseAlphabetDictionary['d'].Player.Play();
_morseAlphabetDictionary['e'].Player.Play();
_morseAlphabetDictionary['f'].Player.Play();
_morseAlphabetDictionary['g'].Player.Play();
_morseAlphabetDictionary['h'].Player.Play();
_morseAlphabetDictionary['i'].Player.Play();
_morseAlphabetDictionary['j'].Player.Play();
_morseAlphabetDictionary['k'].Player.Play();
_morseAlphabetDictionary['l'].Player.Play();
_morseAlphabetDictionary['m'].Player.Play();
_morseAlphabetDictionary['n'].Player.Play();
_morseAlphabetDictionary['o'].Player.Play();
_morseAlphabetDictionary['p'].Player.Play();
_morseAlphabetDictionary['q'].Player.Play();
_morseAlphabetDictionary['r'].Player.Play();
_morseAlphabetDictionary['s'].Player.Play();
_morseAlphabetDictionary['t'].Player.Play();
_morseAlphabetDictionary['u'].Player.Play();
_morseAlphabetDictionary['v'].Player.Play();
_morseAlphabetDictionary['w'].Player.Play();
_morseAlphabetDictionary['x'].Player.Play();
_morseAlphabetDictionary['y'].Player.Play();
_morseAlphabetDictionary['z'].Player.Play();
//.. here goes the rest
}
public static string GetUserInput()
{
string input = Console.ReadLine();
if (!string.IsNullOrEmpty(input))
{
input = input.ToLower();
}
return input;
}
public static string Translate(string input)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (char character in input)
{
if (_morseAlphabetDictionary.ContainsKey(character))
//这就是错误的地方
System.NullReferenceException Message=对象引用未设置为对象的实例。 发生,上面的行。
{
stringBuilder.Append(_morseAlphabetDictionary[character] + " ");
}
else if (character == ' ')
{
stringBuilder.Append("/ ");
}
else
{
stringBuilder.Append(character + " ");
}
}
return stringBuilder.ToString();
}
}
// 任何关于为什么会发生这种情况的想法都非常感谢,非常感谢。
摩尔斯电码映射类
using System.Media;
public class MorseMapping
{
public string MorseLetter { get; set; }
public SoundPlayer Player { get; set; }
public MorseMapping(string letter, string filePath)
{
this.MorseLetter = letter;
this.Player = new SoundPlayer(filePath);
}
}
【问题讨论】:
-
这是不可重现的,因为它的代码不完整。我建议,用调试器逐步完成它
-
这里有几个观察。一是
a与A不相同。这忽略了本地化、编码(unicode)等。您可以使用input.ToLower()解决此问题。二,您的z路径与其他路径不同。 -
哦,我只贴了出错的部分,要不要贴完整代码?谢谢。
-
有趣的开发,我尝试切换_morseAlphabetDictionary['b'].Player.Play();的顺序(所以 a 在底部,z 在顶部)。然后它一直跳过,只播放“a”wav文件。所以它似乎跳过了所有其他执行的 wav 文件到最后一个。想知道为什么会这样。谢谢。
-
我认为我们需要
MorseMapping类背后的代码。根据您所说的,我推测您的所有音频文件都在同时播放,但是Player.Play背后的代码会在程序到达下一行代码时立即停止播放音频播放下一个音频文件。
标签: c# visual-studio console morse-code