uwpvoice-recording

Is it possible to record Audio in minimized state UWP


I need to use voice recording in UWP But if App is minimized then recording doesn't work.

Is there any way to do that, without using restricted capability?


Solution

  • Use ExtendedExecutionSession. It will allow your uwp app to record the audio while minimized. Here is link https://learn.microsoft.com/en-us/windows/uwp/launch-resume/run-minimized-with-extended-execution for details. I have tried and it works fine for me. Add EnteredBackground and LeavingBackground event:

            this.EnteredBackground += AppEnteredBackground;
            this.LeavingBackground += AppLeavingBackground;
    

    When detecting the event, call BeginExtendedExecution and when session is allowed, call capture audio function.

       private async void BeginExtendedExecution()
        {
            ClearExtendedExecution();
    
            var newSession = new ExtendedExecutionSession();
            newSession.Reason = ExtendedExecutionReason.Unspecified;
            newSession.Description = "recording audio";
            newSession.Revoked += SessionRevoked;
            ExtendedExecutionResult result = await newSession.RequestExtensionAsync();
    
            switch (result)
            {
                case ExtendedExecutionResult.Allowed:                   
                    session = newSession;
                    RecordingAudio();
    
                    break;
                default:
                case ExtendedExecutionResult.Denied:                    
                    newSession.Dispose();
                    break;
            }
           
    
        }
    

    You can take https://github.com/microsoft/Windows-universal-samples/blob/master/Samples/ExtendedExecution/cs/Scenario1_UnspecifiedReason.xaml.cs as example.