[英]How to save audio file after recording voice in Xamarin.Forms
I am wondering how to save an audio file in Xamarin forms. 我想知道如何以Xamarin形式保存音频文件。 I can get the location where to save to AppDataDirectory using Xamarin.Essentials:
我可以使用Xamarin.Essentials获取保存到AppDataDirectory的位置:
var mainDir = FileSystem.AppDataDirectory;
But I am not sure how to save it in code, right after recording it using the NateRickard AudioRecorder Plugin: 但是我不确定如何在使用NateRickard AudioRecorder插件录制之后将其保存在代码中:
var recordTask = await recorder.StartRecording();
var audioFile = await recordTastk; //audioFile is of string datatype
Its same as saving/creating any binary file in C#? 它与在C#中保存/创建任何二进制文件一样? Say its same as saving an image file?
说和保存图像文件一样吗? And additionally do I need to use Dependency Injection to save it using platform specific code, or I can manage to only do it from the PCL code?
另外,我是否需要使用依赖注入来使用平台特定的代码来保存它,还是只能通过PCL代码来做到这一点?
The docs explicitly discuss this: 该文档明确讨论了这一点:
Accessing the Recorded File
访问记录的文件
There are multiple ways to access the recorded audio file path:
有多种访问录制的音频文件路径的方法:
- The Task-based API will return the file path when the task completes.
当任务完成时,基于任务的API将返回文件路径。 The Task can be awaited or use standard Task continuation APIs.
可以等待任务,也可以使用标准的任务延续API。
- The Event-based API will return the full path to the recorded audio file in the audioFile parameter of the AudioInputReceived event
基于事件的API将在AudioInputReceived事件的audioFile参数中返回记录的音频文件的完整路径。
handler.处理程序。
- The GetAudioFilePath () method on the AudioRecorderService class will return the recorded audio file path.
AudioRecorderService类上的GetAudioFilePath()方法将返回记录的音频文件路径。
These will all return null in the case that no audio has been recorded yet or no audio was recorded/detected in the last recording session.
如果尚未录制任何音频,或者在上一个录制会话中没有录制/检测到音频,则这些都将返回null。
Once you have the path to the recorded audio file, you can use standard file operations (for native/.NET Standard) and/or a cross platform file system abstraction like PCLStorage to get a stream to the file data.
拥有已录制音频文件的路径后,就可以使用标准文件操作(对于native / .NET Standard)和/或跨平台文件系统抽象(例如PCLStorage)来获取文件数据流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.