根据api文档,音频文件格式必须满足以下要求.
Container -> WAVEnCoding -> PCMRate -> 16KSample Format -> 16 bitChannels -> Mono
继this recipe之后,我成功录制了音频,在玩了一些AndroID文档之后,我也能够实现这些设置:
_recorder.SetoutputFormat(OutputFormat.ThreeGpp);_recorder.SetAudioChannels(1);_recorder.SetAudioSamplingRate(16);_recorder.SetAudioEnCodingBitRate(16000);_recorder.SetAudioEncoder((AudioEncoder) EnCoding.Pcm16bit);
这符合所需音频文件的大多数标准.但是,我似乎无法以实际的“.wav”格式保存文件,我无法验证文件是否实际上是PCM编码.
这是我的AXML和MainActivity.cs:Github Gist
我也跟着this code并将其合并到我的代码中:Github Gist
该文件的规格看起来很好,但持续时间是错误的.无论我录制多长时间,它只显示250毫秒,这导致音频太短.
有没有办法做到这一点?基本上我只想通过Xamarin.AndroID连接到Microsoft的认知说话人识别API.我找不到任何这样的资源来指导自己.
解决方法 录音将Audio Recorder Plugin NuGet Package添加到AndroID项目(如果您使用它们,则添加到任何PCL,netstandard或iOS库).
AndroID项目配置
>在AndroIDMainifest.xml中,添加以下权限:
<uses-permission androID:name="androID.permission.MODIFY_AUdio_SETTINGS" /><uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE" /><uses-permission androID:name="androID.permission.RECORD_AUdio" /><uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission androID:name="androID.permission.INTERNET" />
>在AndroIDManifest.xml中,在< application>< / application>中添加以下提供程序.标签.
<provIDer androID:name="androID.support.v4.content.fileProvIDer" androID:authoritIEs="${applicationID}.fileprovIDer" androID:exported="false" androID:grantUriPermissions="true"> <Meta-data androID:name="androID.support.file_PROVIDER_PATHS" androID:resource="@xml/file_paths"></Meta-data></provIDer>
>在Resources文件夹中,创建一个名为xml的新文件夹
>在Resources / xml内部,创建一个名为file_paths.xml的新文件
>在file_paths.xml中,添加以下代码,将[您的包名称]替换为AndroID项目的包
06002
示例包名称
AndroID录音机代码
AudioRecorderService AudioRecorder { get; } = new AudioRecorderService{ StopRecordingOnSilence = true,PreferredSampleRate = 16000});public async Task StartRecording(){ AudioRecorder.AudioinputReceived += HandleAudioinputReceived; await AudioRecorder.StartRecording();}public async Task StopRecording(){ AudioRecorder.AudioinputReceived += HandleAudioinputReceived; await AudioRecorder.StartRecording();}async voID HandleAudioinputReceived(object sender,string e){ AudioRecorder.AudioinputReceived -= HandleAudioinputReceived; PlaybackRecording(); //replace [UserGuID] with your unique GuID await EnrollSpeaker(AudioRecorder.GetAudiofileStream(),[UserGuID]);}
认知服务说话人识别码
httpClIEnt ClIEnt { get; } = CreatehttpClIEnt(TimeSpan.FromSeconds(10));public static async Task<EnrollmentStatus?> EnrollSpeaker(Stream audioStream,GuID userGuID){ Enrollment response = null; try { var boundryString = "Upload----" + DateTime.Now.ToString("u").Replace(" ",""); var content = new MultipartFormDataContent(boundryString) { { new StreamContent(audioStream),"enrollmentData",userGuID.ToString("D") + "_" + DateTime.Now.ToString("u") } }; var requestUrl = "https://westus.API.cognitive.microsoft.com/spID/v1.0/verificationProfiles" + "/" + userGuID.ToString("D") + "/enroll"; var result = await ClIEnt.PostAsync(requestUrl,content).ConfigureAwait(false); string resultStr = await result.Content.ReadAsstringAsync().ConfigureAwait(false); if (result.StatusCode == httpStatusCode.OK) response = JsonConvert.DeserializeObject<Enrollment>(resultStr); return response?.EnrollmentStatus; } catch (Exception) { } return response?.EnrollmentStatus;}static httpClIEnt CreatehttpClIEnt(TimeSpan timeout){ httpClIEnt clIEnt = new httpClIEnt(); clIEnt.Timeout = timeout; clIEnt.DefaultRequestheaders.AcceptEnCoding.Add(new StringWithQualityheaderValue("gzip")); clIEnt.DefaultRequestheaders.Accept.Add(new MediaTypeWithQualityheaderValue("application/Json")); //replace [Your Speaker Recognition API Key] with your Speaker Recognition API Key from the Azure Portal clIEnt.DefaultRequestheaders.Add("Ocp-APIm-Subscription-Key",[Your Speaker Recognition API Key]); return clIEnt;}public class Enrollment : EnrollmentBase{ [JsonConverter(typeof(StringEnumConverter))] public EnrollmentStatus EnrollmentStatus { get; set; } public int RemainingEnrollments { get; set; } public int EnrollmentsCount { get; set; } public string Phrase { get; set; }}public enum EnrollmentStatus{ Enrolling Training,Enrolled}
音频播放
组态
将SimpleAudioPlayer Plugin NuGet Package添加到AndroID项目(如果您使用它们,netstandard或iOS库).
码
public voID PlaybackRecording(){ var isAudioLoaded = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current.Load(AudioRecorder.GetAudiofileStream()); if (isAudioLoaded) Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current.Play();}总结
以上是内存溢出为你收集整理的c# – 通过Xamarin.Android连接到Microsoft的认知说话人识别API全部内容,希望文章能够帮你解决c# – 通过Xamarin.Android连接到Microsoft的认知说话人识别API所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)