How to display progress indicator while sound recording/playback takes place

Cdn_EuroCdn_Euro Member ✭✭✭
edited December 2018 in Xamarin.Forms

I would like to display a progress indicator while recording sound in my app.
The amount of time allocated for the recording is predefined. I set that up in code, lets say 10 seconds maximum recording time.

I have been trying to make it work right could you please offer some guidance.
Note: I am using the NateRickard AudioRecorder nuget package.

(...)

        if (!recorder.IsRecording)
                {
                    buttonRecord.IsEnabled = false;
                    buttonPlay.IsEnabled = false;

                    DependencyService.Get<IAudioService>().PrepareRecording();

                    // start recording
                    var recordTask = await recorder.StartRecording();

                    buttonRecord.Text = "Stop Recording";
                    buttonRecord.IsEnabled = true;

                    // get the recorded file
                    var recordedAudioFile = await recordTask;

                    // set up progress bar
                    //progressBarRecordTime.Progress = 1.0;
                    //await progressBarRecordTime.ProgressTo(1.0, 10000, Easing.Linear);

                    buttonRecord.Text = "Record";
                    buttonPlay.IsEnabled = true;

                    if (recordedAudioFile != null)
                    {
                        var recordingFileDestinationPath = Path.Combine(FileSystem.AppDataDirectory, AppConstants.CUSTOM_ALERT_FILENAME);

                        if (File.Exists(recordingFileDestinationPath))
                        {
                            File.Delete(recordingFileDestinationPath);
                        }

                        File.Copy(recordedAudioFile, recordingFileDestinationPath);
                    }
                }

(...)

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I think the progress would be calculated as

    1 - ((TotalTimeAllotted-TimeConsumedSofar) / TotalTimeAllotted) to get percentage.

    assume 2 seconds out of 10 have been used

    10 - 2 == 8
    8/10 == .8
    1 - .8 = .2
    So 20% progress

  • Cdn_EuroCdn_Euro Member ✭✭✭
    edited December 2018

    @ClintStLaurent How would i stop the progress bar if the user ends the recording early? If its not too much to ask could you give a code sample?

    Also I am seeing there is need to start a timer when recording starts to keep track of the elapsed time.But how to update the progress bar every second?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I would expect that you're either using commands or MessageCenter messages throughout your app.
    So either StopRecordingCommand binded to a button.
    Or raise a Message for StopRecordingMessage that your AudioRecordingManager class is subscribed to.
    (your class names will of course be different)

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Update after reading your code more closely.

                        buttonRecord.Text = "Stop Recording";
                        buttonRecord.IsEnabled = true;
    ...
                        buttonRecord.Text = "Record";
                        buttonPlay.IsEnabled = true;
    

    Uh... You're not doing any of this like a modern app are you? I'm guessing there is no binding... no commands... no message center usage... MVVM?

  • Cdn_EuroCdn_Euro Member ✭✭✭
    edited December 2018

    @ClintStLaurent Since im relatively new, I am going to convert it to MVVM after the functionality is working well.
    I find it harder to start straight with MVVM. Is this a bad way to go about it by the way? Convert it to MVVM after?

    Half of my app(pages) are MVVM, im in the process to change it all to MVVM.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @ClintStLaurent Thanks very much that is great advice.

Sign In or Register to comment.