C# 调用讯飞实时语音转写

本文介绍了如何使用C#调用科大讯飞的实时语音转写接口,提供了包含StringUtil和EncryptUtil工具类的websocket调用示例代码,帮助开发者进行语音识别集成。
摘要由CSDN通过智能技术生成

关于讯飞云平台申请appId的相关流程在我的另一篇文章java 调用科大讯飞语音听写和语音合成jdk有详细步骤,在此省略,直接上C#调用实时转写的接口代码

websocket调用讯飞接口类:

using IFlyCalc.entity;
using IFlyCalc.utils;
using IFlyCalc.view;
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using WebSocketSharp;

namespace IFlyCalc.net
{
    public class MyWebSocket
    {
        private WebSocket ws;

        private int connectState;

        //用于更新界面的委托
        public delegate bool UpdateRetDelegate(string text, bool beSentence);
        private UpdateRetDelegate updateRetDelegate;

        private bool beEnd;

        private string wsUrl = "ws://rtasr.xfyun.cn/v1/ws";
        private string appId = "*********";//此处请用自己申请的id
        private string apiKey = "*********";//此处请用自己申请的key

        //参数中的page是需要显示结果的页面对象
        public MyWebSocket(MyPage page)
        {
            updateRetDelegate = new UpdateRetDelegate(page.UpdateVoiceResult);
            //可以放在配置文件中方便修改
            //wsUrl = ConfigUtil.IniReadValue("rtasr", "url");
            //appId = ConfigUtil.IniReadValue("rtasr", "appId");
            //apiKey = ConfigUtil.IniReadValue("rtasr", "apiKey");
            //System.Diagnostics.Debug.WriteLine("Read config success");
        }

        public void InitWebSocket()
        {
            try
            {
                if (wsUrl == null || wsUrl.Length == 0)
                {
                    System.Diagnostics.Debug.WriteLine("Read config wsUrl fail");
                    return;
                }

                if (appId == null || appId.Length == 0)
                {
                    System.Diagnostics.Debug.WriteLine("Read config appId fail");
                    return;
                }

                if (apiKey == null || apiKey.Length == 0)
                {
                    System.Diagnostics.Debug.WriteLine("Read config apiKey fail");
                    return;
                }

                if (ws == null)
                {
                    string timeStamp = StringUtil.GetTimeStamp();
                    string md5Code = EncryptUtil.GetMd5Code(appId + timeStamp);
                    string hmacsha1Code = EncryptUtil.HMACSHA1Text(md5Code, apiKey);

                    hmacsha1Code = StringUtil.UrlEncode(hmacsha1Code);
                    ws = new WebSocket(wsUrl + "?appid=" + appId + "&ts=" + timeStamp + "&signa=" + hmacsha1Code + "&pd=edu",
                        onClose: OnClose, onMessage: OnMessage, onOpen: OnOpen, onError: OnError);
                }
                beEnd = false;
                if (connectState == 1)
                {
                    return;
                }
                connectState = -1;

                while (true)
                {
                    if (connectState == 0)
                    {
                        Thread.Sleep(
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值