首页 > 解决方案 > C# 莫尔斯电码应用程序中的声音问题、字符串不返回和音频问题

问题描述

所以我在 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);
            }
    }

标签: c#visual-studioconsolemorse-code

解决方案


对于您的第一个问题,正如其他人建议的那样,您可以使用SoundPlayer.Playasync()播放所有 wav 文件。

对于第二个问题,您需要使用_morseAlphabetDictionary[character].MorseLetter将正确的字母添加到字符串生成器。

这是您可以参考的代码示例。

 class Program
    {
        static void Main(string[] args)
        {
            IntializeDictionary();
            Console.WriteLine("Please input word");
            string input = GetUserInput();
            Console.WriteLine(Translate(input));
            Console.ReadKey();
        }
        static Dictionary<char, MorseMapping> _morseAlphabetDictionary = new Dictionary<char, MorseMapping>()
            { {'a',new  MorseMapping(".-", Path.Combine("D:\\","1.wav")) },
              { 'b', new MorseMapping("-...", Path.Combine("D:\\","2.wav")) },
              { 'c', new MorseMapping("-.-.", Path.Combine("D:\\","3.wav")) }
               //.. here goes  the rest
            };
        private static void IntializeDictionary()
        {
            _morseAlphabetDictionary['a'].Player.PlaySync();
            _morseAlphabetDictionary['b'].Player.PlaySync();
            _morseAlphabetDictionary['c'].Player.PlaySync();
        }
        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))
                {
                    stringBuilder.Append(_morseAlphabetDictionary[character].MorseLetter + " ");
                }
                else if (character == ' ')
                {
                    stringBuilder.Append("/ ");
                }
                else
                {
                    stringBuilder.Append(character + " ");
                }
            }
            return stringBuilder.ToString();
        }
    }
    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);
            this.Player.LoadCompleted += Player_LoadCompleted;
        }

        private void Player_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (this.Player.IsLoadCompleted)
            {
                this.Player.PlaySync();
            }
        }
    }

结果:

在此处输入图像描述


推荐阅读