Is it possible to record Audio in minimized state UWP

Viewed 197

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?

2 Answers

Use ExtendedExecutionSession. It will allow your uwp app to record the audio while minimized. Here is link https://docs.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.

Recording audio in the background is a restricted feature.

Here is the way to add restricted capability:

package.appxmanifest

<?xml version="1.0" encoding="utf-8"?>
<Package
    ...
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="... rescap">
...
<Capabilities>
    <rescap:Capability Name="backgroundMediaRecording"/>
</Capabilities>
</Package>

After adding this capability, the application can continue to record audio when minimized.

However, it should be noted that this is a limited capability. If you want to list in the Microsoft Store, you need to provide additional instructions to describe the reason for using this feature.

Thanks.

Related