12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System.Linq;
- using System.Text.RegularExpressions;
- using Newtonsoft.Json.Linq;
- using TencentCloud.Asr.V20190614;
- using TencentCloud.Asr.V20190614.Models;
- using TencentCloud.Common;
- using TencentCloud.Common.Profile;
- using WingServerCommon.Log;
- namespace WingASRService.Tencent
- {
- public class ASRApi
- {
- private ASRApi(){}
-
- public static (string message, bool isSuccess) CallASRApi(string url, string fileType)
- {
- try
- {
-
-
- Credential cred = new Credential {
- SecretId = "AKIDgDUVFbjbDahYAXeV6RXNxHK8gy0mJabN",
- SecretKey = "XprdARRBgusa3GEwObYFgrmRq0uYv3vY"
- };
-
-
-
-
-
- ClientProfile clientProfile = new ClientProfile();
-
- HttpProfile httpProfile = new HttpProfile();
- httpProfile.Endpoint = ("asr.tencentcloudapi.com");
- clientProfile.HttpProfile = httpProfile;
-
- AsrClient client = new AsrClient(cred, "", clientProfile);
-
- SentenceRecognitionRequest req = new SentenceRecognitionRequest();
- req.ProjectId = 0;
- req.SubServiceType = 2;
- req.EngSerViceType = "16k_zh_medical";
- req.SourceType = 0;
- req.Url = url;
- req.VoiceFormat = fileType;
- req.UsrAudioKey = "any";
-
- var resp = client.SentenceRecognitionSync(req);
- return (message: resp.Result, isSuccess: true);
- }
- catch (Exception e)
- {
- var regMatch = Regex.Match(e.Message,@".*code:([.\w]+)[\s]+.*");
- if(regMatch.Groups.Count>1)
- {
- var errorCode = regMatch.Groups[1].Value;
- if(!string.IsNullOrWhiteSpace(errorCode) && ASRErrorCode.ErrorsDictionary.Keys.Contains(errorCode))
- {
- var errorMessage = ASRErrorCode.ErrorsDictionary[errorCode];
- Logger.WriteLineError($"ASRApi.CallASRApi code:{errorCode} message:{errorMessage}");
- return (message: errorMessage, isSuccess: false);
- }
- }
- Logger.WriteLineError($"ASRApi.CallASRApi error {e}");
- return (message: "Unknow error", isSuccess: false);;
- }
- }
- }
- }
|