Android Question FLAG_SECURE for audio files

Turkan Ergin

Member
Licensed User
I can prevent screen recording during the execution of the application with the following code. However, if I start the screen recorder first and then launch the application, the screen is recorded as a black screen. So far, everything is normal. I also want to prevent the screen recorder from capturing the audio files within my application. Is there a solution for this?

B4X:
#If Java
import android.annotation.TargetApi;
import android.content.Context;
import android.view.WindowManager.*;
public void _onCreate() {
    this.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
#End If
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The best you can do is to detect when screen recording is active and mute the audio.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    RegisterDisplayListener
End Sub

Private Sub RegisterDisplayListener
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim DisplayManager As JavaObject = ctxt.RunMethod("getSystemService", Array("display"))
    Dim listener As Object = DisplayManager.CreateEventFromUI("android.hardware.display.DisplayManager.DisplayListener", "DisplayListener", Null)
    DisplayManager.RunMethod("registerDisplayListener", Array(listener, Null))
End Sub

Private Sub DisplayListener_Event (MethodName As String, Args() As Object) As Object
    If MethodName = "onDisplayAdded" Then
        Log("mute audio!")
    Else If MethodName = "onDisplayRemoved" Then
        Log("unmute audio!")
    End If
    Return Null
End Sub

I've tested it with the built-in screen recorder and it works. It is possible that there are other rare cases where these events will be raised.
 
Upvote 0

Turkan Ergin

Member
Licensed User
First I start the screen recorder and then I open my app. I can't detect the screen recorder. For this reason, it becomes impossible for me to turn off the sound. Do you have a different solution suggestion for this situation? Thanks again for your interest.
 
Upvote 0
Top