C#摩尔斯电码应用程序中的声音出现问题,字符串不返回并且音频出现问题

问题描述

所以我正在用C#编写的莫尔斯电码应用程序在声音和字符串返回方面遇到问题。我设置为在将用户输入转换为摩尔斯电码时为每个摩尔斯电码字母播放repsecive wav文件,但是尽管每个.wav文件都已导入到Visual Studio项目中,但是仅播放字母z的.wav文件。>

此外,当尝试返回翻译后的字符串时,我收到了Null引用异常。这是代码

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);
            }
    }

解决方法

对于第一个问题,正如其他建议一样,您可以使用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("-...","2.wav")) },{ 'c',new MorseMapping("-.-.","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();
            }
        }
    }

结果:

enter image description here