Search code examples
xamarinxamarin.formsuwpcross-platformxamarin.forms.labs

xamarin forms audio recording and upload to a server


Is there any possible way to record an audio and upload to a server using xamarin forms. The best result I got after searching was this https://github.com/HoussemDellai/UploadFileToServer

The library used in the solution supports only Image and Video.

Thanks in advance


Solution

  • Is there any possible way to record an audio and upload to a server using xamarin forms.

    There are many ways realizing this feature. For recording an audio within Xamarin.Forms, you could use Plugin.AudioRecorder to realize. Fore more you could refer the following code.

    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();
        }
    }
    

    For uploading file, you could use the UploadFileToServer that mentioned in your case. And you will get the audio file path in the AudioInputReceived event args.

    private void _recoder_AudioInputReceived(object sender, string e)
    {
        var path = e;
    }