xamarin 表单录音并上传到服务器
xamarin forms audio recording and upload to a server
有没有什么方法可以使用 xamarin forms 录制音频并上传到服务器。
我搜索后得到的最好结果是这个
https://github.com/HoussemDellai/UploadFileToServer
解决方案中使用的库仅支持图像和视频。
提前致谢
Is there any possible way to record an audio and upload to a server using xamarin forms.
实现此功能的方法有很多种。在Xamarin.Forms以内录制一段音频,可以用Plugin.AudioRecorder
来实现。更多的可以参考下面的代码
private AudioRecorderService _recoder;
protected override void OnAppearing()
{
_recoder = new AudioRecorderService
{
StopRecordingOnSilence = true,
StopRecordingAfterTimeout = true,
AudioSilenceTimeout = TimeSpan.FromSeconds(60)
};
_recoder.AudioInputReceived += _recoder_AudioInputReceived;
}
private void _recoder_AudioInputReceived(object sender, string e)
{
// do some stuff
}
private async void Button_Clicked(object sender, EventArgs e)
{
await RecodAudio();
}
private async Task RecodAudio()
{
try
{
if (!_recoder.IsRecording)
{
await _recoder.StartRecording();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
private async void StopButton_Clicked(object sender, EventArgs e)
{
if (_recoder.IsRecording)
{
await _recoder.StopRecording();
}
}
对于上传文件,您可以使用您案例中提到的UploadFileToServer
。您将在 AudioInputReceived
事件参数中获得音频文件路径。
private void _recoder_AudioInputReceived(object sender, string e)
{
var path = e;
}
有没有什么方法可以使用 xamarin forms 录制音频并上传到服务器。 我搜索后得到的最好结果是这个 https://github.com/HoussemDellai/UploadFileToServer
解决方案中使用的库仅支持图像和视频。
提前致谢
Is there any possible way to record an audio and upload to a server using xamarin forms.
实现此功能的方法有很多种。在Xamarin.Forms以内录制一段音频,可以用Plugin.AudioRecorder
来实现。更多的可以参考下面的代码
private AudioRecorderService _recoder;
protected override void OnAppearing()
{
_recoder = new AudioRecorderService
{
StopRecordingOnSilence = true,
StopRecordingAfterTimeout = true,
AudioSilenceTimeout = TimeSpan.FromSeconds(60)
};
_recoder.AudioInputReceived += _recoder_AudioInputReceived;
}
private void _recoder_AudioInputReceived(object sender, string e)
{
// do some stuff
}
private async void Button_Clicked(object sender, EventArgs e)
{
await RecodAudio();
}
private async Task RecodAudio()
{
try
{
if (!_recoder.IsRecording)
{
await _recoder.StartRecording();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
private async void StopButton_Clicked(object sender, EventArgs e)
{
if (_recoder.IsRecording)
{
await _recoder.StopRecording();
}
}
对于上传文件,您可以使用您案例中提到的UploadFileToServer
。您将在 AudioInputReceived
事件参数中获得音频文件路径。
private void _recoder_AudioInputReceived(object sender, string e)
{
var path = e;
}