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

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-